views:

111

answers:

3

I've been using the following bit of code in a cookie cutter fashion, across dozens of classes

    public event PropertyChangedEventHandler PropertyChanged;

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

All of these classes implement INotifyPropertyChanged. To silence my DRY alarms, lately I've been refactoring these classes to inherit my base class PropertyNotifier whose only purpose is to provide NotifyPropertyChanged for classes that inherit from it -- which are the dozens of ViewModel classes in my huge project.

It feels lazy and a bit dirty. Am I hurting performance or breaking good design practices? I figure if change notification was supposed to be this easy, there would be a base class already in the WPF framework that does what my PropertyNotifier class does.

Note that, for a lot of reasons, I've been having performance issues with my UI responsivity -- mostly due to a large number of controls. So I'm looking to trim the fat wherever I can. Any ideas?

+3  A: 

This is a very common base class to have in WPF or Silverlight development and won't noticeably affect performance. The only problem I have with PropertyNotifier as a base class results from being restricted to single inheritance but that tends to be a rare issue with the type of class you'll need it for.

Bryan Anderson
Thank you for the reassurance :D
bufferz
+2  A: 

Yes, thats very common practice. For a large scale application it becomes necessary to have this kind of base class. We had also created a BaseViewModel for the same purpose; we also implemented a lot of common code(across ViewModels) in this base class like logging, displaying error messages, initializing WCF proxy objects etc.

akjoshi
I knew people were using them, but I ended up with a similar BaseViewModel sort of by accident. It's really nice to have a base VM class to put in between WPF and Entity Framework. It saves me a ton of work. I use mine mostly to generate an ObservableCollection<T> from an EF ObjectSet, and keep track of adding/removing new Ts, and a SelectedItem T in addition to the property notification the things you pointed out like initializing, proxy objects, etc
bufferz
+1  A: 

Another implementation is the Model class of the WPF Application Framework (WAF). It does exactly the same.

jbe
Thanks for posting this jbe, that looks like a great framework. It seems as though I've reinvented the wheel!
bufferz