tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/10-dynamically-creating-models-how-to-ensure-proper-dependancy-injectionRobotlegs: Discussion 2018-10-18T16:35:06Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/5841262009-11-13T17:04:40Z2009-11-13T17:10:35ZDynamically creating Model's - how to ensure proper dependancy injection?<div><p>Hello.</p>
<p>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:</p>
<pre>
<code>for(count, count<3; count++)
{
newModel = new MyModel(var1, var2 ...)
}</code>
</pre>
<p>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.</p>
<p>I just wanted to know if there was a method that i was missing
that would help me out in this case.</p>
<p>Thanks<br>
jos</p></div>Jos Yuletag:robotlegs.tenderapp.com,2009-10-18:Comment/5841262009-11-13T17:23:15Z2009-11-13T17:23:15ZDynamically creating Model's - how to ensure proper dependancy injection?<div><p>In your for loop send a command triggering event with the data
and<br>
create the model there. In a command you have full access to
the<br>
injector. It would also remove the need to make your service a
model<br>
factory and provide some encapsulation.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/5841262009-11-13T18:10:19Z2009-11-13T18:10:19ZDynamically creating Model's - how to ensure proper dependancy injection?<div><p>Yes, i see. I've just implemented this in my project, and it
does clean things up quite a bit.</p>
<p>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.</p>
<p>Thanks for the help, it really cleared things up.</p></div>Jos Yuletag:robotlegs.tenderapp.com,2009-10-18:Comment/5841262009-11-13T18:18:32Z2009-11-13T18:18:32ZDynamically creating Model's - how to ensure proper dependancy injection?<div><p>I don't see it as using an event to request action. It is using
an<br>
event as a decoupled command trigger. The service isn't making
a<br>
request, the service is broadcasting thr it has completed it's job.
It<br>
doesn't know or care who/what uses the information. In fact,
the<br>
explicit event is more of a request than a service event that
sends<br>
parsed results, IMO.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/5841262009-11-14T16:29:27Z2009-11-14T16:29:27ZDynamically creating Model's - how to ensure proper dependancy injection?<div><p>Right - there is more going on then in my initial post, but i
see your point. Perhaps a refactoring is in order!</p>
<p>Thanks very much!</p></div>Jos Yule