Git Product home page Git Product logo

Comments (7)

jdewind avatar jdewind commented on August 23, 2024

Hi Thomas,

The object will only act as a singleton if Objection is responsible for constructing it. If you manually allocate it then it will not act as a singleton.

However, if you use a provider with Objection you will get a singleton -- I have verified this.

  [self bindBlock:^(JSObjectionInjector *context) {
    Car *car = [context getObject:[ManualCar class]];
    car.engine = (id)myEngine;
    return (id)car;    
  } toClass:[Car class]];

from objection.

tziem avatar tziem commented on August 23, 2024

Hi Justin,

this only works if your class has a default constructor:

[[ManualCar alloc] init];

But what if your class has to be constructed like this:

[[ManualCar alloc] initWithEngine:myEngine];

~Thomas

from objection.

jdewind avatar jdewind commented on August 23, 2024

Objection uses the default initializer only. You can use the awakeFromObjection method or set the properties after the fact to setup other things.

from objection.

tziem avatar tziem commented on August 23, 2024

Hi Justin,

I've tested your suggestion above but I'm not sure if this will work for me.
Objection will call the provider each time I'm asking for the singleton. Thus all my dependencies also need to be singletons.
What I'm looking for is something like that:

[self bindProvider:[[[CarProvider alloc] init] autorelease] toClass:[Car class]
    lifeCycle:JSObjectionInstantiationRuleSingleton];

~Thomas

from objection.

jdewind avatar jdewind commented on August 23, 2024

Tziem,

If you are creating your own provider there isn't any reason you couldn't make the object it builds a singleton. The provider can create and hold a reference to an instance of whatever object you are trying to create. It can then return that same object anytime it is called on to provide an object.

The provider mechanism is built for flexibility, so you can control whatever it is you like within the context of the provider.

from objection.

tziem avatar tziem commented on August 23, 2024

But this means that there are more than one managed contexts. Objection holds a global context and a provider can also hold a context.
In Java I'm using Google Guice for DI.
There I can do something like that:

bind(TransactionLog.class).to(InMemoryTransactionLog.class).in(Singleton.class);

Neither TransactionLog nor InMemoryTransactionLog knows anything about singletons.
Providers can also have dependencies which will be managed by the injector.

~Thomas

from objection.

jdewind avatar jdewind commented on August 23, 2024

Indeed,

If you need use of a custom initializer with custom dependencies you are outside of Objection's normal workflow anyhow. Objection is delegating the construction of an object to a provider, much like it delegates to an Objection Module. There isn't any reason, however, that you couldn't create your own custom provider (singleton vs normal) and objection module class (new binding method that specifies lifecycle) that does this for you.

Objection does not initialize objects using 'constructors' (initializer methods), but instead uses property injection via Key-Value coding. Hence, it is awkward to try and use Objection with customer initializers.

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.