Inject AbstractModel

Kimak's Avatar

Kimak

22 Apr, 2012 08:32 PM

Hello there,

I have an architecture modular application where i need to use a Model who extends an AbstractModel.
I need to Inject one instance of the Model as AbstractModel in a command shared by two modular context.

Exemple:
Module1Model extends AbstractModel
Module2Model extends AbstractModel

Module1Context do this:
injector.mapSingletonOf(AbstractModel, Module1Model);

Module2Context do this:
injector.mapSingletonOf(AbstractModel, Module2Model);

Everything works fine and my CommonCommand can Inject the AbstractModel in the 2 context correctly.
But my application is pretty huge and i don't want to cast the model in each context...

Exemple in Module1Command.as

[Inject]
public var model:AbstractModel;

override public function execute():void
{
  //Module1Model(model).data ...
}

Did you have any options to Inject a class with two different type on different commands? Or this is a SwiftSuspender limitation ?
Thks.

  1. Support Staff 2 Posted by Ondina D.F. on 24 Apr, 2012 12:43 PM

    Ondina D.F.'s Avatar

    Hi Kimak,

    I’m not sure I understand how you are mapping the models and commands.
    It sounds like you are mapping the models in the context of each module, and the CommonCommand in the shell’s context. Then you want to trigger the same command from each module?
    Later on you mention a Module1Command , which lets me think there is a Module2Command as well. Are you mapping them in the shell’s context or within each module’s context?

    Anyway, if you had
    2 Commands SharedCommandOne and SharedCommandTwo, both mapped in the shell’s context and 2 Models, SharedModelOne and SharedModelTwo, also mapped in the shell’s context, both extending AbstractModel, then you can use named injection to distinguish one Model from another:

    injector.mapSingletonOf(AbstractModel,SharedModelOne,"modelOne");
    injector.mapSingletonOf(AbstractModel,SharedModelTwo,"modelTwo");

    moduleCommandMap.mapEvent(SharedEvent.OPEN_SHARED_COMMAND_ONE,SharedCommandOne);
    moduleCommandMap.mapEvent(SharedEvent.OPEN_SHARED_COMMAND_TWO,SharedCommandTwo);

    SharedCommandOne:

    [Inject (name="modelOne")]

    public var model:AbstractModel;

    SharedCommandTwo:

    [Inject (name="modelTwo")]

    public var model:AbstractModel;

    Would this work for you?

    Did you have any options to Inject a class with two different type on different commands? Or this is a SwiftSuspender limitation ?

    A mapping like this:
    injector.mapSingletonOf(AbstractModel, Module1Model);
    injector.mapSingletonOf(AbstractModel, Module2Model);

    is ambiguous. The first injection would be overwritten by the second one. The injector can not know what to inject into what and when. Named injections let the injector distinguish between the 2 mappings.
    Cheers,
    Ondina

  2. Support Staff 3 Posted by Ondina D.F. on 24 Apr, 2012 01:43 PM

    Ondina D.F.'s Avatar

    And in case you want to avoid named injections, you can use something like this:
    http://knowledge.robotlegs.org/discussions/questions/590-using-mult...

  3. Ondina D.F. closed this discussion on 05 May, 2012 01:21 PM.

  4. Kimak re-opened this discussion on 09 May, 2012 08:14 PM

  5. 4 Posted by Kimak on 09 May, 2012 08:14 PM

    Kimak's Avatar

    I choose the wrapper solution on your link, to avoid named injections.

    Thank you for the answer !

  6. Support Staff 5 Posted by Ondina D.F. on 14 May, 2012 12:04 PM

    Ondina D.F.'s Avatar

    No problem:)

  7. Ondina D.F. closed this discussion on 14 May, 2012 12:04 PM.

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