Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding INotifyPropertyChanged to Model?

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;}
}

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

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

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.

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?

Thanks!

like image 513
Asaf Avatar asked Feb 14 '13 07:02

Asaf


People also ask

Can a model implement INotifyPropertyChanged?

Sometimes it is acceptable to have the model implement the INotifyPropertyChanged interface. For example if the model has a lot of properties to be visualized and you want to avoid to implement a lot of code (proxy properties) in the viewmodel for exposing such model properties. Save this answer.

How do you implement INotifyPropertyChanged?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

Should ViewModel implement INotifyPropertyChanged?

So the ViewModel needs to implement INotifyPropertyChanged . To further validate that decision, you can see that the View should be completely ignorant of the Model and it should only know of its ViewModel .

Does ObservableCollection implement INotifyPropertyChanged?

INotifyCollectionChanged . ObservableCollection implements INotifyCollectionChanged which will notify us when the collection has a mutated state.


1 Answers

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.

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

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.

like image 70
Dennis Avatar answered Sep 28 '22 14:09

Dennis