Dynamically creating Model's - how to ensure proper dependancy injection?
Hello.
I have a class which is receiving data over the web. From this data i then need to generate a number of Model classes. eg:
for(count, count<3; count++)
{
newModel = new MyModel(var1, var2 ...)
}
I want the newModel to have all the proper dependancies injected, but since i'm manually instancing it, and i don't have access to the ContextView's injector, i have to manually set its dependancies.
I just wanted to know if there was a method that i was missing that would help me out in this case.
Thanks
jos
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Joel Hooks on 13 Nov, 2009 05:23 PM
In your for loop send a command triggering event with the data and
create the model there. In a command you have full access to the
injector. It would also remove the need to make your service a model
factory and provide some encapsulation.
3 Posted by Jos Yule on 13 Nov, 2009 06:10 PM
Yes, i see. I've just implemented this in my project, and it does clean things up quite a bit.
The only thing i don't like, is using events to request actions. I know there has been some discussion on the list about this. To make it as explicit as possible, i've created an event specifically for this - CreateNewPlaylistEvent. I hope by being as explicit as possible with the events (and their names), it should keep things understandable.
Thanks for the help, it really cleared things up.
Support Staff 4 Posted by Joel Hooks on 13 Nov, 2009 06:18 PM
I don't see it as using an event to request action. It is using an
event as a decoupled command trigger. The service isn't making a
request, the service is broadcasting thr it has completed it's job. It
doesn't know or care who/what uses the information. In fact, the
explicit event is more of a request than a service event that sends
parsed results, IMO.
5 Posted by Jos Yule on 14 Nov, 2009 04:29 PM
Right - there is more going on then in my initial post, but i see your point. Perhaps a refactoring is in order!
Thanks very much!