Search code examples
c#design-patternsobserver-patternmediator

Communication between two ViewModels with same Model and two views


I have a C# Application with two different views. Each view has an own ViewModel. The ViewModels access the same Model. The Views need the data from the same Model in a different format. The ViewModels handle the formatting and validation.

Both ViewModels should be able to communicate with each other in some way. For example if ViewModel1 updates something in the Model ViewModel2 should also update his View. The ViewModels don't have to know each other, they should just get synced when one side changes something.

I found some old posts aout the Mediator pattern. Is this still the way to go? I think the Observer pattern would not work here. The only alternative I could think of was to create a Interface on both ViewModels which let them talk to each other.


Solution

  • i think what your looking for is Message-passing system for your View Models.if you what them to be loosely coupled you can use events to implement one by yourself. also there is Event Aggregator class in prism library that do the same.some classes subscribe and some publish their messages.