Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting from one base class that implements INotifyPropertyChanged

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 responsiveness -- mostly due to a large number of controls. So I'm looking to trim the fat wherever I can. Any ideas?

like image 947
bufferz Avatar asked Sep 22 '10 15:09

bufferz


People also ask

How is INotifyPropertyChanged implemented?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

How to inherit multiple classes in vb net?

Unlike languages that allow multiple inheritance, Visual Basic allows only single inheritance in classes; that is, derived classes can have only one base class. Although multiple inheritance is not allowed in classes, classes can implement multiple interfaces, which can effectively accomplish the same ends.

How to Inherits class in vb net?

Class methods and properties must be marked as Overridable to allow inheritance. A class that inherits from a base class must mark methods that are being overridden with the Overrides keyword. Finally, if a class cannot be inherited from, this must be indicated with the NotInheritable keyword.

What is INotifyPropertyChanged?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .


1 Answers

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.

like image 176
Bryan Anderson Avatar answered Nov 02 '22 09:11

Bryan Anderson