Bookmark and Share

Observing ObservableObject

When creating any component that must be bound, it is essential to implement the INotifyPropertyChanged interface. Repeating yourself is a bad thing, and although you can’t get around it in all situations, sometimes it’s preferable to merely inherit from a class that implements this interface for you. In a preview for the upcoming release of MVVM Light, Laurent Bugnion introduced a class call NotifyPropertyChanged.

It does follow to name the class this, as it is just a concrete implementation of the INotifyPropertyChanged interface. In fact, I did that same thing in my own projects. However, in a recent blog post, Laurent indicated he has renamed the class ObservableObject. This is simple, elegant, and like most things that are simple and elegant: brilliant!

My first reflections over this name were, “why not just name it Observable?” I’m not very fond of sticking Object at the end of a class name as it usually tells me nothing. In this case, there is a defined Observer pattern. Here is a diagram for it.

ObserverPattern

In the observer pattern, The interaction is between the observer and the observed. In this diagram, the observed (or what I would call observable) is the subject. An observer subscribes to the subject, and the subject notifies the observer when it desires. This is oftentimes preferable to polling for changes.

I based this on the classic diagram, but there are other ways to implement this using interfaces and generics. One such interface IObservable<T> in the .NET Framework 4. And this leads me to why it would be wrong to name it Observable rather than ObservableObject. Here’s a basic implementation of ObservableObject (link to the MVVM Light version) .

public class ObservableObject : INotifyPropertyChanged
{              
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Using INotifyProperyChanged is basically stating that you’re making this object’s state observable. It isn’t an IObservable<string>, or anything of that nature. It’s more like an IObservable<this>, without the work of implementing that particular interface. It is an ObservableObject, and it deserves to be named such. INotifyProperyChanged appears to named after the method it contains. A better name would be IObservableObject.

blog comments powered by Disqus

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
Telerik Developer Evangelist, C# MVP

JustCode

Telerik .NET Ninja

 

INETA Community Speakers Program

 

MVP - Visual C#

 

Friend of RedGate

World Map

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.