Inject values into a model?

dan's Avatar

dan

11 Jul, 2014 09:30 PM

I was conducting an interview the other day and this candidate was saying that they typically inject values into their model... ie

GameModel.as
[Inject] 
public var player1:Player;
[Inject]
public var player2:Player;

This struck me as odd and I'm not used to doing things this way. I'm curious though---anyone else work this way? reasons?

Thanks!
Dan

  1. Support Staff 1 Posted by Ondina D.F. on 14 Jul, 2014 11:33 AM

    Ondina D.F.'s Avatar

    Hi Dan,

    First of all, injecting player1 and player2 like this wouldn't work. To differentiate between the 2 you'd need named injections:

    [Inject (name="somePlayer")] 
    public var player1:Player;
    
    [Inject (name="anotherPlayer")] 
    public var player2:Player;
    

    Then, the question is, what is Player? A view class, another model, a VO, something else?

    It's hard to say if injecting Player into GameModel is a good or a bad idea just from looking at the few lines of code. The use case is not specific enough.

    Injecting dependencies into a Model is not a bad thing per se.

    It depends on the role of the injected class: injecting Views, Mediators, Services, or other mutually dependent Models into a Model is not a recommended practice.
    But, it also depends on the specific use case and the overall structure of the project. Question to ask: How is GameModel 'manipulating' the 2 instances of the Player class? What is the role of the GameModel? Is it holding state, or is it manipulating some data? Does injecting the Player class break encapsulation? Is GameModel less reusable because of the Player dependency? Is it harder to test the application because of Model's dependencies?

    Or, is there an entity system/framework involved, where maybe GameModel is not a "typical" robotlegs model?

    You probably know more about the concrete use case, so you might be right in thinking that injecting the Player into the GameModel was problematic. Tell us more about the project or/and the way you'd solve the Player problem, so that I, or anyone else, could give you a more specific answer.

    Ondina

  2. 2 Posted by bojamunje on 28 Jul, 2014 12:19 PM

    bojamunje's Avatar

    I usually inject singleton signals into models, to avoid injecting models into mediators.

    but I inject them as setters to avoid future mistakes

    [Inject]
    public function set signal(value:Signal):void{
      _signal = value;
    }

  3. 3 Posted by dan on 23 Aug, 2014 12:11 AM

    dan's Avatar

    Thanks for the comments and good questions. This was just an informal open ended coding challenge to open up OO and design pattern discussions.

  4. dan closed this discussion on 23 Aug, 2014 12:12 AM.

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