开发者

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>.

开发者_StackOverflow

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜