Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObservableObject or INotifyPropertyChanged on ViewModels

I was curious what was the best thing to do with ViewModels. Is it better to implement the interface INotifyPropertyChanged or to derive from ObservableObject.

ObservableObject class implements INotifyPropertyChanged and do some of the boring code like RaisePropertyChanged.

INotifyPropertyChanged require to implement PropertyChanged event.

From my point of view it seems more logical to use ObservableObject, but in most of the tutorial they implement INotifyPropertyChanged interface on their ViewModel.

Do you think it's for the sake of simplicity or there's a logical reason ?

like image 893
Xavier Avatar asked Apr 10 '12 17:04

Xavier


People also ask

Should model or Viewmodel implement INotifyPropertyChanged?

You should always have the Model implement INotifyPropertyChanged and this is just a mistake which would be corrected if this were developed from a code example to an application.

What is the use of INotifyPropertyChanged in C#?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.

What is an observable object C#?

The ObservableObject is a base class for objects that are observable by implementing the INotifyPropertyChanged and INotifyPropertyChanging interfaces. It can be used as a starting point for all kinds of objects that need to support property change notifications.


1 Answers

ObservableObject is part of Microsoft.Practices.Composite.Presentation - i.e. Prism. It's also been implemented in MVVM Light and MVVM Foundation.

INotifyPropertyChanged is part of System.ComponentModel - i.e. it's in the core libraries.

So, if you are not already including Prism or one of the other frameworks I'd stick with INotifyPropertyChanged. There seems to be little point in including it just to get this one class.

like image 119
ChrisF Avatar answered Oct 31 '22 12:10

ChrisF