views:

50

answers:

4

I have seen apps implementing this interface everywhere. In many cases, we could use the new property syntax like

public int Foo { get; set; }

which I like very much. However, in order to implement this interface, this one has to turn into 10 lines or so. This makes the code very cluttered, and I'm not sure if it also hurts performance.

Can someone explain when this interface is really necessary?

+1  A: 

It's necessary if you want to subscribe to notifications that a property has changed. If you don't need to (and no 3rd party libraries need to) you don't have to implement this interface.

Kirk Woll
it's for databinding notifications on properties
Steven A. Lowe
+1  A: 

You implement that interface when your data object needs to advertise (notify) that a property has changed. This is especially important when using databinding, and very useful when using the Observer pattern.

Check here for an approach i follow when i have lots of properties that need to notify of changes.

slugster
A: 

This interface is necessary when working with libraries or other features within the framework that expect it.

The most common is when using a UI framework like WPF with its data binding. In order for the UI to "know" that your property has changed, so it can reflect the contents of a TextBox, for example, the object to which its bound needs to either be a DependencyObject and the property needs to be a DependencyProperty, or you need to implement INotifyPropertyChanged.

This is what makes 2-way databinding work properly.

That being said, it's common to implement this on a base class, which can make your subclass implementation only be a few lines per property. (You can't use automatic properties, but you potentially only need a couple of extra lines in the setter if you use a base class.)

Reed Copsey
A: 

An alterative is to use Microsoft's ReactiveExtensions (Rx) framework to wrap all up all the plumbing work into a single Observable<T> object.

See this StackOverflow question and answers for an example of how to do this.

Judah Himango