将 INotifyPropertyChanged 添加到模型? [英] Adding INotifyPropertyChanged to Model?

查看:13
本文介绍了将 INotifyPropertyChanged 添加到模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 wpf MVVM(基于棱镜)应用程序中遇到了一些设计问题,很高兴得到您的建议.我的模型很简单:

I'm facing some design questions in my wpf MVVM (Prism based) application, would be happy to get your advice. My model is very simple:

public class Customer
{
   public string FirstName {get;set;}
   public string LastName {get;set;}
}

如您所见,我的 Model 类没有任何 INotifyPropertyChnaged 支持.我还有用于 CustomerDetails 屏幕的 ViewModel,支持 INotifyPropertyChanged.

As you can see, I don't have any INotifyPropertyChnaged support for my Model class. I also have ViewModel for the CustomerDetails screen, that support INotifyPropertyChanged.

public class CustomerDetailsViewModel:INotifyPropertyChanged /*Or NotificationObject*/
{
   /*INotifyPropertyChanged event */
   private Customer item;
   public Customer Item
   {
      get{return item;}
      set
      {
         item=value; 
         //Raise PropertyChanged
         //Set IsDirty to true
      }
   }
}

在我看来,我正在使用绑定到 Item.FirstName 和我正在更新的 ViewModel.我的问题是 - 由于只有 FirstName 属性通过视图更新,并且模型本身不支持 INotifyPropertyChanged,因此没有调用 Item setter,并且 IsDirty 保持等于 false(因此不会更新 IsDirty 通知用户界面).

In my view, i'm using binding to the Item.FirstName and my ViewModel being updated. My problem is - since only the FirstName property is being updated via the View, and the Model itself does not support INotifyPropertyChanged, hence the Item setter not being called, and the IsDirty remains equal to false (and therefore does not update the IsDirty notification on the UI).

我知道我可以在模型中支持INotifyPropertyChanged,然后注册到视图模型中的Item.PropertyChanged事件中,实际上将IsDirty设置为true,但是——由于我也在使用 CodeFirst,并且我的 Model 类在我的 ServerSide 和我的客户端之间共享(不使用添加服务引用),我不想将 INotifyPreoprtyChanged 内容添加到我的服务器端.

I know I can support INotifyPropertyChanged in the model, and then register to the Item.PropertyChanged event in the view model, and actually set the IsDirty to true, But - Since I'm also using CodeFirst, and my Model class shared between my ServerSide and my client side (Not using Add Service Reference), I don't want to add the INotifyPreoprtyChanged stuff to my server side.

我正在考虑创建新项目,该项目将使用 T4 模板逐个复制我的所有实体(作为客户),并为每个模型添加 INotifyPropertyChanged 支持.这似乎是合理的还是不合理的?还有其他建议吗?

I'm considaring creating new project, that will use T4 templates to copy one by one all my Entities (as Customer) and adding INotifyPropertyChanged support to each and every model. Is that something that seems reasonable or not? any other suggestions?

谢谢!

推荐答案

Option1.
将在客户端和服务器 (DTO) 之间传输的实体与作为客户端模型的实体分开.在模型中实现 INPC.使用这些实体之间的映射.

Option1.
Separate entities, which being transferred between client and server (DTO), from entities, which are models on the client side. Implement INPC in models. Use mapping between these entities.

选项 2.
绑定视图仅查看模型属性.制作视图模型属性,它包装相应的模型属性.

Option2.
Bind view to view model properties only. Make view model properties, which wrap corresponding model properties.

选项 3.
是前两个选项的混合.不要在视图模型中聚合模型.使用模型和视图模型之间的映射.制作与模型属性对应的视图模型属性.

Option 3.
Is a mix of first two options. Do not aggregate model in view model. Use mapping between model and view model. Make view model properties, which correspond to model properties.

这篇关于将 INotifyPropertyChanged 添加到模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆