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?
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.
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With