Model injecting another model and vice versa

Amnesiac's Avatar


23 Feb, 2012 08:52 AM

From robotlegs guide:
"Never create mutually dependent models ModelA injects ModelB, and ModelB injects ModelA: uh oh! This is a strong sign that
you’ve got a design flaw. In many cases your code will blow up the first time either of
these models is used (when the injector tries to instantiate and fulfil the injections and
gets itself in a knot). Even if your code doesn’t actually explode, there’s usually a better
solution, often involving a parent model that uses both of these models independently."

I have ModelA which need data from ModelB, but in some cases ModelB need data from ModelA, how should I do that without injecting models mutually?

I've found some solutions that says I should pass data from ModelA to ModelB in Command. It means to pass a whole model instance and call ModelA methods from ModelB, or select needed data from ModelA, and pass it as parameters to ModelB method.

  1. 1 Posted by Stray on 23 Feb, 2012 09:13 AM

    Stray's Avatar

    "a parent model that uses both of these models independently."

    This parent model can handle the wiring between the two.

    Or you can do semi-manual wiring with setter injection:

    // in ModelA
    public function set modelB(value:ModelB):void
    _modelB = value;
    _modelB.modelA = this;

    The other option is to let the models 'use' each other via signals between them rather than direct access - it depends whether your models really need to use each other.

    Or - sometimes it's a sign that you need to break out a 3rd class - lets call it ModelC - which is composed into both A and B - so they have access to the same instance - with a smaller subset of the API. (Crucially ModelC would be independent and not need A/B injected into it).

    Without knowing the problem domain we can only describe options, but there should be something here that fits.



  2. 2 Posted by Amnesiac on 23 Feb, 2012 09:19 AM

    Amnesiac's Avatar

    I have for example 10 instances of ObjectA that are stored in ModelA, and every of ObjectA have 10 ObjectB stored in ModelB.

    Now, i have two listsView - when U select some ObjectA in first listView You see in second listView with all ObjectB that are childs of ObjectA.

    But every ObjectB in second listView need to show also some ObjectA (depended on type variable in ObjectB).

    So left list show ObjectA, right show ObjectB (with some ObjectA). So they need data from themselves.

    Maby my command should take ObjectBVO from ModelB, and then get another data from ModelA and join it to ObjectBVO ??

  3. 3 Posted by Stray on 23 Feb, 2012 09:34 AM

    Stray's Avatar

    Hi there,

    In that situation I tend to use another object: ObjectAB that represents only the relationship between the two.

    Does that make sense? Then both ObjectA and ObjectB have a reference to ObjectAB that joins the two together.


  4. 4 Posted by Amnesiac on 23 Feb, 2012 09:46 AM

    Amnesiac's Avatar

    I don't get it...

    Lets say I select item from left list:

    • ObjectA selected, ListsViewMediator dispatch event SELECT_A_OBJECT,

    • ObjectASelectCommand sets current objectA in ModelA,

    • ModelA dispatch OBJECT_A_SELECTED

    • ListsViewMediator gets that event and want to get right list elements so dispatch LOAD_B_OBJECTS

    • LoadBObjectsCommand call modelB.loadObjects()

    • ModelB dispatch B_OBJECTS_LOADED with collection of ObjectBVO

    • Now i got allmost all i need...

    I don't know what next... cuz collection of ObjectBVO is now dataprovider for right list, and it's ok, but ObjectBVO dont have complete data, becouse right list ItemRenderer need also some ObjectA and ObjectBVO didn't provided them becouse modelB.loadObjects() couldn't get them becouse didn't have modelA injected instance to get them.

  5. 5 Posted by Stray on 23 Feb, 2012 10:25 AM

    Stray's Avatar

    Yes - I manage these relationships by only holding a list of related keys in each object, and having a utility class that has access to both full sets and figures out which objects to show in the dependent list.

    It's not really a Robotlegs issue - more just a data-lists issue, don't you think?

  6. Ondina D.F. closed this discussion on 29 Feb, 2012 11:20 AM.

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

Keyboard shortcuts


? 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