Closed diljimp closed 12 years ago
No, there isn't a way to do that. The reason for this is that Objection, at runtime, determines the property type and from there can lookup what type of object needs to be fetched from the injector context.
Without any type information we'd have to fall back on conventional methods that other dynamic languages use (e.g. the property name 'loginService' is mapped to "LoginService" followed by looking up the class by that name). Objective-C does have a bit of an identity crisis with regard to typing -- since dynamic typing can be used. But I want to keep Objection consistent in how it looks up types.
What you could do is define the property with the implementing protocol: id<ILoginService> loginService
I have just started using Objection in a Mac OS X project. I'm finding a problem when trying to inject a dependency that implements a protocol.
I have a LoginService class that implements the protocol ILoginService. I keep an object that implements ILoginService inside another class named SessionManager. Now I register LoginService class with ILoginService using a Module in Objection. However, when trying to automatically inject the property that implements ILoginService in SessionManager I get the following error. "Cannot find an instance that is bound to the protocol 'ILoginService' to assign to the property 'loginService'"
My code looks like follows.
========== start of code ==============================
@protocol ILoginService
@end
@interface LoginService : NSObject
{
}
@end
@implementation LoginService
// implementation code
@end
@protocol ISessionManager
@end
@interface SessionManager : NSObject
{
id loginService;
}
@property(strong) id loginService;
@end
@implementation SessionManager
objection_register_singleton(SessionManager) objection_requires(@"loginService") @synthesize loginService;
// implementation code
@end
@interface MyInjectorModule : JSObjectionModule
@end
@implementation MyInjectorModule
@end
// inside Application main method JSObjectionInjector *injector = [JSObjection createInjector:[[MyInjectorModule alloc] init]]; [JSObjection setDefaultInjector:injector]; id sessionManager = [[JSObjection defaultInjector] getObject:@protocol(ISessionManager)]; // I get the above mentioned error when executing this line
========= end of code ================================
If I rather use the concrete class name in the LoginManager property declaration in SessionManager (i.e. LoginService *loginService) it works. However I need to keep the loginService property with the type id so that the concrete class can be changed in the future.
Is there a way to achieve this in Objection?