增加INotifyPropertyChanged的到模型?模型、INotifyPropertyChanged

2023-09-04 02:39:50 作者:骑着蜗牛找期待

我面临着一些在我的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;}
}

正如你所看到的,我没有为我的模型类的任何INotifyPropertyChnaged支持。 我也有视图模型为CustomerDetails的屏幕,支持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和我的视图模型被更新。 我的问题是 - 由于仅姓属性被经由视图更新,并且模型本身不支持INotifyPropertyChanged的,因此项目设定器没有被调用,并且IsDirty保持等于假(并因此不上更新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为真,但是 - 因为我还使用codeFirst,和我的我的服务器端和我的客户端之间共享的模型类(不使用添加服务引用),我不想给inotify的preoprtyChanged的东西添加到我的服务器端。

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.

我considaring创建新的项目,将使用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?

谢谢!

推荐答案

选项1。照片 独立的实体,其中客户机和服务器(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.