Skip to content
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

Closed
Undistraction opened this issue Sep 21, 2011 · 7 comments
Closed

Comments

@Undistraction
Copy link

Currently if I want the injector to supply a singleton to one of my classes I have the following options:

  1. [JSObjection registerClass:theClass lifeCycle: JSObjectionInstantiationRuleSingleton];

Which registers a class to be created when first asked for and shared as a singleton.

  1. [jSObjectionModuleinstance registerEagerSingleton:clazz];

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.

@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

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.

@Undistraction
Copy link
Author

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:

An injector manages its own object context. Which means that a singleton is per injector and is not necessarily a true singleton.

So is there any way to have persisted singletons and be able to add bindings after the initial injector is created?

@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

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.

@Undistraction
Copy link
Author

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?

@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

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];
}

@Undistraction
Copy link
Author

That's Perfect. Really appreciate you taking the time to put me on the right path.

@jdewind
Copy link
Contributor

jdewind commented Sep 21, 2011

No problem. You are using Objection in a relatively complex manner and I can see where you would have questions.

@jdewind jdewind closed this as completed Sep 21, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants