Question

I'm new with IoC, and i'm trying to follow best practices in applying it. I have a ListViewModel from where i want to create a new EditViewModel that accepts the injection of the object that has to be modfied.

I thought the solution could be sending a message containing this object to the IoC Container, that will pass it to the EditViewModel.

Can I do it? Is there any better way?

Thank you very much!

Was it helpful?

Solution

If you want your view model to create other view models, then you want to create a view model factory. Your ListViewModel would take this view model factory as a dependency (via constructor injection for example), then when it wanted to create an edit view model, it could do:

var editViewModel = this.EditViewModelFactory.Create(modelToEdit);

or

var editViewModel = this.EditViewModelFactory.Create();
editViewModel.SetModel(modelToEdit);

You want the edit view model factory to be resolved via your IoC container. Containers such as Castle Windsor and Ninject allow automatic factory registration. In this case, you only define the interface for the factory, and the container actually instantiates a type that implements this interface for you.

You certainly don't want your application talking to your container via a mediator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top