Injections beginners problems.

jiri's Avatar

jiri

22 Apr, 2010 01:19 PM

Hello,

I am new to Robotlegs and started exploring it since
yesterday. I run into a small problem that I can't really understand.

So i have a AccountModel class which holds a AccountsManager
class.In this AccountsManager class I create and hold instances of
BaseAccount. BaseAccount is extending Actor and implements an Iaccount
interface.
I instantiate each BaseAccount in the AccountModel like so:


public function addAccount(accountId:String):void{      
    var account:IAccount = new BaseAccount( accountId  );
    manager.addAccount( account );
    // dispacth it over the central bus, in the context
    dispatch( new AccountEvent( AccountEvent.ACCOUNT_ADDED ) );

}

In my AppContext I have the following.


    injector.mapClass( BaseAccount , BaseAccount);
    injector.mapSingleton(AccountManager);
    injector.mapSingleton(AccountsModel);

The reason why I have the BaseAccount extend Actor is that I want
to dispatch a framework event when the state of a BaseAccount is
changed/updated. But the BaseAccount shows that eventDispatcher is null.

So I thought I inject the model and use the model its dispatcher to
send an event. But adding this (code below) in the BaseAccount also results in
accountModel to be null.


    [Inject]
    public var accountModel:AccountsModel;

Clearly I am missing something, but I can't figure out what. Some help
would be much appreciated.

Jiri

  1. Support Staff 1 Posted by Shaun Smith on 22 Apr, 2010 02:14 PM

    Shaun Smith's Avatar

    Hi Jiri,

    For injections to be satisfied, instances need to either be:

    1. constructed by the Injector - with injector.instantiate()
    2. processed by the Injector - with injector.injectInto()

    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.

    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.

    The short answer is that you could change your code like so:

    [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 ) );
    }
    

    This would ensure that the account's dependencies are satisfied. Notice that I'm injecting the IInjector itself into the AccountModel.

    The long answer is that using the injector in this way is usually a sign of a problematic design.

    I wonder why you need both an AccountModel and an AccountManager?

    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.

    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.

    I hope that makes sense. Let me know if anything needs more explanation.

    Cheers,
    Shaun

  2. 2 Posted by jiri on 22 Apr, 2010 02:27 PM

    jiri's Avatar

    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.

    Cheers,

    Jiri

  3. Support Staff 3 Posted by Shaun Smith on 22 Apr, 2010 02:38 PM

    Shaun Smith's Avatar

    Cool, glad to be of assistance :)

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