How to work with PropertyChanged’s smelly name string

Karl posted an interesting article recently INotifyPropertyChanged – How to remove the Property Name String Code Smell.

It'a nice implementation and the resulting code certainly looks a whole better. As Karl notes the biggest problem this approach faces is one with performance.

Notably, the proposed implementation is a lot slower when there are listeners wired up to the INotifyPropertyChanged plumbing.

The performance could be improved by caching the StackTrace based on an accompanying static key for each property, e.g.
private static readonly object _namePropertyKey = new object();
public string Name
    get { return _name; }
        _name = value;
However, this is probably just as prone to runtime errors in case somebody uses the wrong key with the wrong property.

With respect to the importance of performance then Karl is right to note that your users can only type so fast. But in some models and viewmodels, say containing a few hundred points that will be represented on a chart. That hit in performance could REALLY hurt.

My approach to this problem is to leave the smelly string where it is and automate the testing process using the Automatic Class Tester. This will automaticaly test that your properties are wired up appropriately and, if your class implements INotifyPropertyChanged it will also check that the appropriate PropertyChanged string name is fired.

Originally posted by Josh Twist on 2nd August 2009 here.

Skip to main content