When to resist using [Inject]?
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?
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Stray on 30 Sep, 2011 04:34 PM
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?
3 Posted by prettymuchbryce on 30 Sep, 2011 05:12 PM
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?
4 Posted by Stray on 30 Sep, 2011 05:29 PM
Perfect :)
5 Posted by prettymuchbryce on 30 Sep, 2011 06:38 PM
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.
6 Posted by Stray on 30 Sep, 2011 06:44 PM
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
7 Posted by visnik on 12 Oct, 2011 10:24 PM
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.
Ondina D.F. closed this discussion on 23 Dec, 2011 08:55 AM.