Net Dev
Net Dev

Reputation: 416

MvvM ViewModel to ViewModel Communication

I have two view models ViewModelA and ViewModelB they both use a common DataServiceA to retrieve a User Setting called Theme. ViewModelB can change that setting and save it to the database. When this occurs the ViewModelA does not update that theme value.
I have been trying to figure out the proper course of action to allow it to update it as well and have come up with a few options:

  1. Make the DataService raise a property notification when a value is saved (I don't like this)
  2. Use MessengerService to keep them in sync. (I am having issues running into a messaging loop)
  3. Pass one ViewModel to the other

Any ideas?

Upvotes: 0

Views: 1035

Answers (2)

BenjaminPaul
BenjaminPaul

Reputation: 2931

Sounds like a good case for using events, if they both have access to DataServiceA could you not raise an event when the theme is changed?

So.. in the DataServiceA, "OnThemeChanged" event is raised when the theme is modified and the ViewModels can subscribe to the event and update their content when it is raised.

Upvotes: 1

Blachshma
Blachshma

Reputation: 17395

If both of them use the same instance of DataServiceA, I don't think it's such a bad idea that DataServiceA will also implement INotifyPropertyChanged and raise an event when the Theme property changes.

But if you don't like that idea, another option is to use the EventAggregator and publish the event. I personally think the first option is quite sufficient but it's your call...

Upvotes: 0

Related Questions