Git Product home page Git Product logo

Comments (7)

jdewind avatar jdewind commented on August 23, 2024

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.

from objection.

Undistraction avatar Undistraction commented on August 23, 2024

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?

from objection.

jdewind avatar jdewind commented on August 23, 2024

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.

from objection.

Undistraction avatar Undistraction commented on August 23, 2024

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?

from objection.

jdewind avatar jdewind commented on August 23, 2024

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

from objection.

Undistraction avatar Undistraction commented on August 23, 2024

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

from objection.

jdewind avatar jdewind commented on August 23, 2024

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

from objection.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.