1

I have three models and viewmodels that depend on each other for validation. Additionally, the models must react to each others' changes. For example if ModelA.Prop1 is modified, ModelB.Funds must repopulated from the DB.

How do you implement this in MVVM?

LostInComputer
  • 15,188
  • 4
  • 41
  • 49

1 Answers1

1

How you solve this might be dependent on your setup.

If you cause or detect the change in a ViewModel you could initiate the refresh from within the ViewModel. Because the ViewModel is allowed to know about the Model you could do this while still sticking to the MVVM pattern. For communication between ViewModels, e.g. between the ViewModel of ModelA and the ViewModel of ModelB you could use a messaging system such as the message broker in MVVM light.

If the Models are wrapped in repositories you could make the repositories responsible for syncing the Models. That way you would be able to delay synchronization for as long as possible.

A side note I would like to make is that if the Models are so dependent you might be looking at a single Model instead of two. Check that first! If you feel the need to setup a lot of synchronization and dependencies between the two models it is a clear sign that you might be looking at a single Model. Do not confuse a Model with a class; a Model can have multiple classes.

Emond
  • 50,210
  • 11
  • 84
  • 115