Question

The dependent ViewModel gets injected via the constructor (IoC container).

Example: ProductSelectionViewModel uses ShoppingBasketViewModel.

Is this a common practice or is this THE recommended way? I don´t think so...

How should it be done right?

Should the view use the 2 ViewModels? Mediator pattern? Event driven?

Personally I don´t like the last one.

Was it helpful?

Solution

There's nothing wrong with a view model having a direct reference to another view model, if it is a required dependency, then injecting it via the constructor is fine.

If you wish for a view model to be able to create new instances of another view model, then injecting a view model factory type would be the way to go.

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