I have been tasked with a new project. I am familiar with WPF and mvvm and have completed a few in production projects utilizing it.
This latest request is simple – but requires sharing an incoming datasource (that is refreshed every 30 seconds) between two views. Here are a few of the ways I have thought of achieving this:
1) Create one shared view model between the two views. I don’t really like this option – while they do use the same data source, what the two views do with the data is quite different.
2) Create two view models where one of them is the “parent” view model and one is the child. The child view model registers for a message from the parent. The parent view model sends that message. While this one would be the easiest to get up and running – I feel like there would be a lot of code revolving around the state of the application (was it just loaded? Is it already loaded and this is a data refresh?).
Ideally, what I would like is some sort of broadcast pattern, where the data is retrieved and broadcast – each viewmodel is created and then grabs the broadcasted message (via some type of messenger). Then on subsequent updates, the messenger lets the viewmodels know an update is coming.
I’ve looked at the mediator pattern (using messaging) but this doesn’t quite meet what I need. I don’t want to create the viewmodels in order to register for a message.
I am aware that there probably isn’t some “silver bullet” solution. Just looking for ideas on the best way to approach this.