Dynamically creating Model's - how to ensure proper dependancy injection?

Jos Yule's Avatar

Jos Yule

13 Nov, 2009 05:04 PM


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.


  1. Support Staff 1 Posted by Joel Hooks on 13 Nov, 2009 05:23 PM

    Joel Hooks's Avatar

    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.

  2. 2 Posted by Jos Yule on 13 Nov, 2009 06:10 PM

    Jos Yule's Avatar

    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.

  3. Support Staff 3 Posted by Joel Hooks on 13 Nov, 2009 06:18 PM

    Joel Hooks's Avatar

    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.

  4. 4 Posted by Jos Yule on 14 Nov, 2009 04:29 PM

    Jos Yule's Avatar

    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!

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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