Automatic DI in Classes of non-type Actor, Command or Mediator

r.moorman's Avatar

r.moorman

27 Aug, 2010 09:03 AM

Hi,

Is it possible to inject dependencies in other classes than the Actor, Command or Mediator (and it's viewComponents) by the following instantiation:

var myClass: MyClass = new MyClass;

Looking at the source code of Robot Legs, I couldn't find any 'injector.instantiate( type of Actor)', just the XML configuration which is describing the Actor dependencies.

My goal is to automatically inject dependencies in classes which are not on the Display List and are not constructed by the 'injector.instantiate' method. From my perspective an Actor is that kind of Class, am I right?

Kind regards,

R. Moorman

  1. 1 Posted by Jason Dias on 27 Aug, 2010 09:32 AM

    Jason Dias's Avatar

    Hello,

    If you want to inject into something the injector needs to know about it,
    you can do this a few ways, one of them is like so

        [Inject]
        public var injector:IInjector;

        var myClass: MyClass = new MyClass;
        injector.injectInto( myClass );

    So once you have done this the dependencies for myClass will be satisfied.
    Notice the injector itself is injected, so the class that does the
    injectInto needs to be injectable as well.

    Hope this helps.

    Jason

  2. 2 Posted by Stray on 27 Aug, 2010 09:39 AM

    Stray's Avatar

    Hi Rob,

    injector.injectInto() should give you what you're describing - assuming that the injections have been mapped before you do that.

    Let us know if that doesn't do what you need,

    Thanks,

    Stray

  3. 3 Posted by r.moorman on 27 Aug, 2010 09:41 AM

    r.moorman's Avatar

    Hi Jason,

    And suppose I wouldn't call the injectInto method, just by creating a instance of my Class and yet it gets it's dependencies.

    The Actor Class is an example Class for this. Nowhere in the framework you can create Actors trough methods, or there are listened to.

    Thanks for the quick reply by the way,

    Rob

  4. 4 Posted by Stray on 27 Aug, 2010 09:50 AM

    Stray's Avatar

    Hi Rob,

    no, there's no way for the system to automagically inject a class you create using new Thing(). Otherwise the framework would have to somehow listen for creation of every new class and check it for Injection requirements - which I'm sure you understand would be unviable.

    You will not use new Actor() in the framework - the Actor class is to be extended for Models and Services which require the shared eventbus. That is all 'Actor' provides. So - generally your models and services will be instantiated via the injector because they are most likely singletons / singletonOf. Or they just use injector.instantiate() to come to life.

    So - if you don't ask the injector to inject somehow - whether that's by instantiate or injectInto or by mapping as a singleton etc, the injection will not happen. The framework assumes that you don't need injection unless you ask for it.

    Hope that make sense (give some thought to how 'magic' injection would work and you'll quickly realise it is a dead end).

    Thanks,

    Stray

  5. 5 Posted by r.moorman on 27 Aug, 2010 09:58 AM

    r.moorman's Avatar

    Hi Stray,

    Already got the solution as you said.

    I found a workaround to get these classes injected. If you map for example a singleton of the custom class (let's call it MyClass), you are able to retrieve it in a Command, Mediator or mapped viewComponent. When the MyClass is injected, it also recieves injection. Only then you can accomplish my goal.

    I tried it with instantiating an Actor Class (not receive it by injection). Result; no injections.

    When I inject/mapped this Actor Class in a Mediator, it's dependencies are also injected.

    Thanks for the replies, my point is clear now. It's not possible, unless you write yourself a factory class or something, or just use the injectInto and injector.instantiate methods.

    Rob

  6. r.moorman closed this discussion on 27 Aug, 2010 10:00 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac