简体   繁体   English

如何在MVVM中双向同步模型和视图模型?

[英]How to synchronize Model and View Model bidirectional in MVVM?

It's a simple question and I searched the Internet for hours without success... 这是一个简单的问题,我在互联网上搜索了数小时却没有成功...

I have a model and a view model with one property. 我有一个模型和一个具有一个属性的视图模型。 To make this property viewable in the view, I use a view-model-object which should automatically be generated from the model-object and vice versa. 为了使该属性在视图中可见,我使用了一个view-model-object,它应该从model-object自动生成,反之亦然。 Of course, the following code will throw an StackOverflowException, because the updating of the model-object in the model causes an update of the view-model-object in the view-model and this causes an update of the model-object in the model and so on... 当然,以下代码将引发StackOverflowException,因为模型中模型对象的更新导致视图模型中视图模型对象的更新,并且这导致模型中模型对象的更新等等...

class ModelObject
{
  ...
}

class ViewModelObject
{
  ...
}

class Model : INotifyPropertyChanged
{
  private ModelObject modelObject = new ModelObject();

  ...

  public ModelObject ModelObject
  {
    get
    {
      return this.modelObject;
    }
    set
    {
      this.modelObject = value;
      this.NotifyPropertyChanged("ModelObject");
    }
  }
}

class ViewModel : INotifyPropertyChanged
{
  private ViewModelObject viewModelObject = new ViewModelObject();
  private Model model = new Model();

  ...

  public ViewModel()
  {
    this.model.PropertyChanged += new PropertyChangedEventHandler(this.propertyChangedEvent);
  }

  public ViewModelObject ViewModelObject
  {
    get
    {
      return this.viewModelObject;
    }
    set
    {
      this.viewModelObject = value;
      this.model.ModelObject = new ModelObject(...);
      this.NotifyPropertyChanged("ViewModelObject");
    }
  }

  private void propertyChangedEvent(object sender, PropertyChangedEventArgs e)
  {
    if (e.PropertyName.Equals("ModelObject"))
    {
      this.ViewModelObject = new ViewModelObject(...);
    }
  }
}

What is the common way to synchronize these two objects? 同步这两个对象的常用方法是什么?

There is no silver bullet. 没有银弹。 As model is a representation of the database and viewmodel is more closer to the interface, there is always some business logic needed to convert the model to view model and vice versa. 由于模型是数据库的表示形式,而视图模型更接近接口,因此始终需要一些业务逻辑才能将模型转换为视图模型,反之亦然。

I usually have two methods in my view model class - SyncModel(ViewModel viewModel) and SyncViewModel(Model model) 我的视图模型类中通常有两种方法SyncModel(ViewModel viewModel)SyncViewModel(Model model)

One more suggestion - 另一个建议-

Model should not implement INotifyPropertyChanged. 模型不应实现INotifyPropertyChanged。 The view model should implement this as its bound to the user interface. 视图模型应将其实现为与用户界面的绑定。 Why does the model ever need to change? 为什么需要更改模型? It represents whats in the db. 它代表数据库中的内容。 You can refresh it but why do you need change notifications for the model? 您可以刷新它,但是为什么需要模型更改通知?

Edit: MVVM: Binding to Model while keeping Model in sync with a server version 编辑: MVVM:绑定到模型,同时保持模型与服务器版本同步

Hard reference. 硬参考。 Each class having a reference to another, listens to property change event and updates itself accordingly. 每个类都有另一个引用,侦听属性更改事件并相应地进行更新。

Observer Pattern - Have an observer class, each class register itself with an observer, observer listens for any changes and updates all its subscribers. 观察者模式-拥有一个观察者类,每个类都向观察者注册,观察者侦听任何更改并更新其所有订阅者。

There's also an event aggregator which might be useful. 还有一个事件聚合器可能会有用。

If you want a deferred update, an isDirty property would be required. 如果要推迟更新,则需要isDirty属性。 You know your application better, google for more articles and choose wisely. 您对自己的应用程序了解得更多,请谷歌搜索更多文章并明智地选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM