When to resist using [Inject]?

prettymuchbryce's Avatar

prettymuchbryce

30 Sep, 2011 04:25 PM

I am reading the RL book, and I notice that there is a lot of encourage to resist reaching for [Inject] all the time. I have a question about a specific case.

Say I have a model that uses a service. I have the service mapped using Injector.MapClass. Why not simply have this service be a protected var of the model? What would be the point of injecting it? If I don't inject it, and this service has it's own injections, will they still be fulfilled?

  1. Support Staff 1 Posted by Stray on 30 Sep, 2011 04:34 PM

    Stray's Avatar

    Hrm... now "A model that uses a service" does not compute in my head :)

    My models dispatch events, and sometimes a command picks up one of those events and then acts on a service.... but the model itself would never 'contain' the service.

    Perhaps you have a really interesting use case for this? Is there something interesting about this model that means it needs to use the service directly?

    Or maybe when you say 'service' you don't mean the same as me...

    Say a little more?

  2. 2 Posted by prettymuchbryce on 30 Sep, 2011 05:12 PM

    prettymuchbryce's Avatar

    Okay I see what you mean. I think my initial instinct in the way I was designing this is off. So I basically have a form that needs to read an XML file and then fill in some fields based on the XML.

    Correct me if I'm wrong. You're saying the model dispatches event saying it needs to fill its vars with the data from the XML -> a command gets this event and creates the service -> The service gathers all the XML info -> The service or command dispatches another event saying its done loading and attaches the data to this event -> The model (the one that previously used the service) catchs this event and takes that data and fills in its variables?

  3. 3 Posted by Stray on 30 Sep, 2011 05:29 PM

    Stray's Avatar

    Perfect :)

  4. 4 Posted by prettymuchbryce on 30 Sep, 2011 06:38 PM

    prettymuchbryce's Avatar

    Okay great. I have one more loosely related question. As I am setting this up I realize that there are typically packages for events in model, service, and view. How do I determine where an event class should reside if it interacts with both a model and a view? Is it whichever fires the event? Whichever is listening for the event? Is it up to the developers discretion? I could use some clarity on this.

  5. 5 Posted by Stray on 30 Sep, 2011 06:44 PM

    Stray's Avatar

    Hi Bryce - do a quick search and you'll find this question has been discussed in depth quite a few times - should give you plenty to chew on!

    Shout if you can't find anything relevant and I'll point you at the right posts,

    Stray

  6. 6 Posted by visnik on 12 Oct, 2011 10:24 PM

    visnik's Avatar

    prettymuchbryce,

    the answer is actually in the RL book, cant remember what page off hand, but I remember reading it in there. Place it with the class that owns the event, the one dispatching it. If i find what page it is on, I will post it.

  7. Ondina D.F. closed this discussion on 23 Dec, 2011 08:55 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