What is the best way to keep ViewModel Collection in sync with Model
In my application, I have a model object that contains a collection lets say List<Chair>
that gets updated frequently. ViewModel has an ObservableCollection<ChairViewModel>
that wraps List<Chair>
.
Since List<Chair>
gets updated frequently, I need to keep ObservableCollection<ChairViewModel>
in sync with it so that every change in List<Chair>
gets reflected on WPF UI.
Any ideas?
It really depends on the specifics of your application as you'll be dealing with trade-offs for which only you'll have sufficient information to choose between.
Some approaches I've used in the past are:
- Have the model provide fine-grained notifications of changes and have the VMs subscribe to those notifications
- Have a service layer that your view models call into to get model data. The service layer can periodically update its cache from the model
- Delay communications with the model as long as possible and attempt to automatically synchronize a local cache with the model
There are many approaches, but choosing the right one for your application is the key.
精彩评论