Take the 2-minute tour ×

I'm a student learning MVVM, and I'm working on a WPF application. My problem arises when I have to instantiate a ViewModel_2 from ViewModel_1. ViewModel_2 depends on an XService, that is registered in the IoC Container. So when i ask the container to resolve a ViewModel_2, it creates the service it needs, injects it into the constructor, and provides the ViewModel.

My question is: I've always read that the container should not be referenced by any class but the bootstrapper that manages its lifecycle, so who gives ViewModel_2 to ViewModel_1?

The only solution I can think is put a Lazy in the ViewModel_1's ctor, and access it when needed, but what if i have more than a single VM to manage from there??

I hope you can help me. Thank you very much, and Happy New Year! =)

share|improve this question
add comment

1 Answer

As i understand, you have ViewModel_1 that contains property ViewModel_2. And you need to inject this ViewModel_2 with DI. In this simple case, you can inject ViewModel_2 in constructor of ViewModel_1.

You should give us more details about "what if i have more than a single VM to manage from there". What VM did you mean? Where is "there"? If you post code of ViewModel_1 and ViewModel_2 it will help to understand your problem.

share|improve this answer
Thank you Kirill for your reply. The VM2 is not necessarily a VM1's property, but just a VM that is created in VM1 and used by it to do something. I wonder if I have more than just VM2 to create in VM1, am I obliged to add a ctor parameter for each additional VM or is there a simpler solution? Thank you again. –  Nalesso Sergio Dec 30 '12 at 6:08
And sorry but what do you mean with "you can inject ViewModel_2 in controller of ViewModel_1"? –  Nalesso Sergio Dec 30 '12 at 6:10
*constructor. Its misspelling –  Kirill Bestemyanov Dec 30 '12 at 6:13
In general, if you need to inject some independent dependencies, you should add all of them to constructor parameters. In some special cases, when you dependencies are linked to each other, it can be other solutions apecial for this cases. –  Kirill Bestemyanov Dec 30 '12 at 6:20
I wonder how people in the same situation behave when they work on a project much bigger than mine, and need to call many more ViewModels =( –  Nalesso Sergio Jan 2 '13 at 9:54
add comment

Your Answer


