2

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.

timmkrause
  • 3,367
  • 4
  • 32
  • 59

1 Answers1

3

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.

devdigital
  • 34,151
  • 9
  • 98
  • 120