tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/81-injections-beginners-problemsRobotlegs: Discussion 2013-04-28T10:02:17Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/15143672010-04-22T14:14:44Z2010-04-22T14:14:44ZInjections beginners problems.<div><p>Hi Jiri,</p>
<p>For injections to be satisfied, instances need to either be:</p>
<ol>
<li>constructed by the Injector - with
<code>injector.instantiate()</code><br></li>
<li>processed by the Injector - with
<code>injector.injectInto()</code></li>
</ol>
<p>The CommandMap and MediatorMap do this automatically when
Commands and Mediators are created, so it is very rare that you'll
need to do this yourself.</p>
<p>In your case you are manually creating a new BaseAccount, but
nothing is processing it - nothing is looking at it to determine
and satisfy it's dependencies.</p>
<p>The short answer is that you could change your code like so:</p>
<pre>
[Inject]
public var injector:IInjector;
public function addAccount(accountId:String):void {
var account:IAccount = new BaseAccount( accountId );
injector.injectInto( account );
manager.addAccount( account );
// dispacth it over the central bus, in the context
dispatch( new AccountEvent( AccountEvent.ACCOUNT_ADDED ) );
}
</pre>
<p>This would ensure that the account's dependencies are satisfied.
Notice that I'm injecting the IInjector itself into the
AccountModel.</p>
<p>The long answer is that using the injector in this way is
usually a sign of a problematic design.</p>
<p>I wonder why you need both an AccountModel and an
AccountManager?</p>
<p>Also, I don't think that BaseAccount should extend Actor in
order to inform the app of changes to its state. The purpose of a
Model class is to encapsulate data/state, provide an API for the
application to alter that state through, and to dispatch events
when that state changes. By allowing other parts of the app to
directly manipulate Accounts (without going through the model) you
bypass this encapsulation.</p>
<p>Instead, all changes to an account should happen through the
AccountModel via its public API. The AccountModel will then
dispatch events in response to these actions.</p>
<p>I hope that makes sense. Let me know if anything needs more
explanation.</p>
<p>Cheers,<br>
Shaun</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/15143672010-04-22T14:27:57Z2010-04-22T14:30:18ZInjections beginners problems.<div><p>Shaun, thank you for the super fast reply! I understand that the
design looks problematic, it probably is. But I am just playing
around for now, to get a grip on Robotlegs. Anyways, thanks for the
clear explaination, it does work now and brings me another step
closer to understanding RL.</p>
<p>Cheers,</p>
<p>Jiri</p></div>jiritag:robotlegs.tenderapp.com,2009-10-18:Comment/15143672010-04-22T14:38:33Z2010-04-22T14:38:33ZInjections beginners problems.<div><p>Cool, glad to be of assistance :)</p></div>Shaun Smith