-
Notifications
You must be signed in to change notification settings - Fork 223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for registering a Singleton class mapped to an Interface #8
Comments
You can do this very thing using a provider. [self bindBlock:^(JSObjectionInjector *context) {
return [context getObject:[MyClass class]];
} toProtocol:@protocol(MyProtocol)]; I introduced the provider pattern to solve these kind of unique problems. With the intention of integrating them into Objection if the pattern is recurring. |
That's Great. Thanks. Please can you clarify something for me. In my use-case I need to be able to use the functionality available from Modules numerous times during the application lifecycle. For example I might want to use bind:toClass during initialisation and again at some point later. Given that the only time a module can be used is when creating an injector, I was planning on using a fresh module and injector each time it was needed, safe in the knowledge that all injectors shared the same context, so bindings declared in a module passed to one injector would be available to subsequent injectors, however in the README you state:
So is there any way to have persisted singletons and be able to add bindings after the initial injector is created? |
You could have a global injector that manages the singletons that you want to persist using a provider. [self bindBlock:^(JSObjectionInjector *context) {
return [gGlobalContext getObject:[MyClass class]];
} toClass:[MyClass class]]; Though I'm not familiar enough with your particular situation to say whether this is what you are looking for. |
Ok. Let me put it another way: Is it possible to register an eager singleton in a module passed to Injector 1, and have subsequent injectors supply the same instance of the same class to objects they create? In other words are singletons only respected if the same injector that consumed the module in which they were registered? |
Subsequent injectors would have different instances. In order to achieve what you are doing you'd have to configure an injector with singletons that you want to persist across other injectors and keep a reference to it. Subsequent injectors would have to be configured with a different module that created provider bindings to get access to the global singletons. For example, @interface ApplicationModule : JSObjectionModule
@end
@implementation ApplicationModule
- (void)configure {
[self bindInstance:someObject toClass:[MyClass class]];
... More bindings ...
}
@end
@interface EagerSingletonBindingsApplicationModule : ApplicationModule
@end
@implementation EagerSingletonBindingsApplicationModule
- (void)configure {
[super configure];
[self registerEagerSingleton:[Singleton class]];
}
@end
@interface ProviderBindingsApplicationModule : ApplicationModule
@end
@implementation ProviderBindingsApplicationModule
- (void)configure {
[super configure];
[self bindBlock:^(JSObjectionInjector *context) {
return [[JSObjection globalInjector] getObject:[Singleton class]];
} toClass:[Singleton class]]
}
@end
- (void)someBootsrappingMethod {
EagerSingletonBindingsApplicationModule *eagerSingletonBindingsModule = [[[EagerSingletonBindingsApplicationModule alloc] init] autorelease];
JSObjectionInjector *injector = [JSObjection createInjector:eagerSingletonBindingsModule];
[JSObjection setGlobalInjector:eagerSingletonBindingsModule];
} |
That's Perfect. Really appreciate you taking the time to put me on the right path. |
No problem. You are using Objection in a relatively complex manner and I can see where you would have questions. |
Currently if I want the injector to supply a singleton to one of my classes I have the following options:
Which registers a class to be created when first asked for and shared as a singleton.
Which registers a class to be created immediately and shared as a singleton.
But I have no way of having a singleton mapped to a protocol. No way to say; 'Whenever asked for a dependency of SomeProtocol satisfy it with a (singleton) instance of SomeClass.
The text was updated successfully, but these errors were encountered: