Adrian
Adrian

Reputation: 3438

Implement INotifyPropertyChanged on models or viewmodels?

It's been a while since I dove into WPF and so I'm spending some time reacquainting myself with some of the methodologies.

Regarding this MSDN Article - INotifyPropertyChanged should be implemented in your model classes. This is a bit confusing because on other articles I see INotifyPropertyChanged being implemented on viewmodels. Some even implement it on both. Are implementation details just project specific or is there a "best practice" way to implement INotifyPropertyChanged in wpf and mvvm?

The one issue I see with adding change notification to model classes is when using an ORM like entity framework and the database first approach. Each a db change occurs and your models get updated, any code you have written in them gets overwritten. I realize I could probably create another partial class to mitigate this but that seems too cumbersome and hacky.

My general question is: What is the best practice when implementing change notification? Implementation on models or view models?

Upvotes: 0

Views: 227

Answers (2)

Dave
Dave

Reputation: 493

INotifyPropertyChanged is commonly used throughout the ViewModel to inform the View that some value somewhere has changed and that it should rebind to the applicable data.

For changes in the model:

The ViewModel should know that a property has changed in the Model simply because the ViewModel changed the property.

Upvotes: 0

user4856537
user4856537

Reputation:

My advice is use it where you need it. You would implement it on both models and view models.

If you have a property in your ViewModel that you want to track in your xaml, you wouldn't create a model to encapsulate it, because it would be overkill.

Implementing INotifyPropertyChanged for ViewModel is absolutely the common way in MVVM pattern.

You can find this practice inside several MVVM libraries, like MVVM Light, which has a ViewModelBase class that you inherit from your ViewModels in order to have INotifyPropertyChanged implemented de facto.

Upvotes: 1

Related Questions