MVVM: Should a ViewModel have a member to another ViewModel?

853 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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.