ObservableObject or INotifyPropertyChanged on ViewModels

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

ObservableObject class implements INotifyPropertyChanged and executes some boring codes, such as RaisePropertyChanged .

INotifyPropertyChanged is required to implement the PropertyChanged event.

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

Do you consider this for the sake of simplicity or a logical reason?

+7
source share
3 answers

ObservableObject is part of Microsoft.Practices.Composite.Presentation - that is, prisms. It has also been implemented by MVVM Light and the MVVM Foundation .

INotifyPropertyChanged is part of System.ComponentModel - that is, in the main libraries.

So, if you haven't enabled Prism or one of the other frameworks yet, I would stick with INotifyPropertyChanged . It seems to make little sense to include it just to get this class.

+14
source

I would go so far as to say that if you implement a lot of ViewModels, there is a lot of plumbing code needed to implement INotifyPropertyChanged, and you would be better off creating your own base ViewModel class or using one of the ones provided by the MVVM card ( MVVM Light , which is my basis of choice).

Otherwise, you really are not very “dry” (do not repeat yourself).

Change - Randomly Thought

Keep in mind that an ObservableObject, as a rule, simply facilitates the implementation of the same basic functions of the ViewModel class, providing you with INPC, but leaving some other things you can use in the full ViewModel.

+1
source

As a rule, ceteris paribus, I suggest going with an approach where you implement interfaces compared to ancestor classes. You can implement as many interfaces as you like, but you only get one parent, so I prefer to use my parent wisely. In the case when I can get what I want to implement the interface, I prefer to leave my options open for inheritance later.

0
source

All Articles