tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/263-automatic-di-in-classes-of-non-type-actor-command-or-mediatorRobotlegs: Discussion 2018-10-18T16:35:16Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/27200822010-08-27T09:03:11Z2010-08-27T09:31:35ZAutomatic DI in Classes of non-type Actor, Command or Mediator<div><p>Hi,</p>
<p>Is it possible to inject dependencies in other classes than the Actor, Command or Mediator (and it's viewComponents) by the following instantiation:</p>
<pre><code>var myClass: MyClass = new MyClass;</pre></code>
<p>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.</p>
<p>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?</p>
<p>Kind regards,</p>
<p>R. Moorman</p></div>r.moormantag:robotlegs.tenderapp.com,2009-10-18:Comment/27200822010-08-27T09:32:20Z2010-08-27T09:32:20ZAutomatic DI in Classes of non-type Actor, Command or Mediator<div><p>Hello,</p>
<p>If you want to inject into something the injector needs to know about it,<br />
you can do this a few ways, one of them is like so</p>
<pre><code>[Inject]
public var injector:IInjector;
var myClass: MyClass = new MyClass;
injector.injectInto( myClass );</code></pre>
<p>So once you have done this the dependencies for myClass will be satisfied.<br />
Notice the injector itself is injected, so the class that does the<br />
injectInto needs to be injectable as well.</p>
<p>Hope this helps.</p>
<p>Jason</p></div>Jason Diastag:robotlegs.tenderapp.com,2009-10-18:Comment/27200822010-08-27T09:39:04Z2010-08-27T09:39:04ZAutomatic DI in Classes of non-type Actor, Command or Mediator<div><p>Hi Rob,</p>
<p>injector.injectInto() should give you what you're describing - assuming that the injections have been mapped before you do that.</p>
<p>Let us know if that doesn't do what you need,</p>
<p>Thanks,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/27200822010-08-27T09:41:01Z2010-08-27T09:41:02ZAutomatic DI in Classes of non-type Actor, Command or Mediator<div><p>Hi Jason,</p>
<p>And suppose I wouldn't call the injectInto method, just by creating a instance of my Class and yet it gets it's dependencies.</p>
<p>The Actor Class is an example Class for this. Nowhere in the framework you can create Actors trough methods, or there are listened to.</p>
<p>Thanks for the quick reply by the way,</p>
<p>Rob</p></div>r.moormantag:robotlegs.tenderapp.com,2009-10-18:Comment/27200822010-08-27T09:50:31Z2010-08-27T09:50:31ZAutomatic DI in Classes of non-type Actor, Command or Mediator<div><p>Hi Rob,</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>Hope that make sense (give some thought to how 'magic' injection would work and you'll quickly realise it is a dead end).</p>
<p>Thanks,</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/27200822010-08-27T09:58:20Z2010-08-27T09:58:20ZAutomatic DI in Classes of non-type Actor, Command or Mediator<div><p>Hi Stray,</p>
<p>Already got the solution as you said.</p>
<p>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.</p>
<p>I tried it with instantiating an Actor Class (not receive it by injection). Result; no injections.</p>
<p>When I inject/mapped this Actor Class in a Mediator, it's dependencies are also injected.</p>
<p>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.</p>
<p>Rob</p></div>r.moorman