One of the things you do regularly when working with Silverlight and/or WPF is implementing INotifyPropertyChanged.
In short: this interface exposes an event (PropertyChanged) that is raised when a property of the object changes its value. This allows other parts of your application to respond to changes in the class that implements the interface. For instance: a TextBlock shows the value of the Name property of a Person object and you want the TextBlock to update the text as soon as the Name property changes its value. So the Text property of the TextBlock need to be notified of changes to the Name property of the Person.
Here is a very basic implementation of the Person class:
As you can see in lines 11-14: if at least a single object subscribed to the PropertyChanged event the class raises the event passing itself as the source and the name of the property that changed. BTW: notice that in order to have the event raised you should use the property setter when writing to the property and not the field (_name) unless you want to suppress the event.
There a couple of things wrong with this implementation. I’ll fix them one by one.
First: when there are more properties we need to repeat the same code over and over in the setters so let’s factor out the raising of the event:
The protected OnPropertyChanged method raises the event and can easily be called from within any setter in the class and its descendants.
Now a tricky error in this implementation. Suppose that between lines 17 and 19 the last subscriber to the event unregisters. This would clear the event’s invocation list and would cause a null reference exception in line 19. To make the method thread-safe we use the following construction:
It isn’t that obvious that this solves the concurrency problem. But it does. Here is how: the invocation list is immutable so as soon as there is a change to the list (someone registers or unregisters) a NEW list is created. So when we first copy (p in line 3) the reference, both references point to the same invocation list but when an object (un)registers the event will point to a different list but the copy still contains the original list.
On to the next problem. As you can see we specify the name of the changed property in the call to the OnPropertyChanged method as a literal string. This can cause problems when making spelling errors or when renaming the property without updating the literal string. Even worse: it fails silently.
Although the event is raised it will communicate the wrong property name to the event subscriber.
So let’s solve this by using reflection:
The setter is a method that is prefixed with “set_” so that is why we cut off those first four characters.
This seems fine and it does work however we now pay a penalty due to the fact that reflection is slow. So let’s add an optimization:
This way (line 6) we only raise the event when there really is a change of the value. This addition is an optimization that is a good idea even when we do not use reflection. Now let’s try to get rid of the reflection:
The Debug comes to the rescue: Assert and its parameters will only be evaluated and called when running the Debug version of the assembly is being executed. So this solution requires unit tests to make sure that the assertions are evaluated before building the release version.
When renaming the property and forgetting to change line 8 with the correct value an exception will be thrown when running the debug build version.
Our simple property has grown quite a bit and now requires a lot of typing. I see two valid solutions for that:
- An item template for a class that implements INotifyPropertyChanged and a snippet for a property that raises the PropertyChanged event.
- Code generation using a tool or T4 templates (I might add a post on that later)
I picked the first option for the time being. My template:
And the snippet:
When adding a new class to a WPF or Silverlight application (e.g. when adding a class to the ViewModel in MVVM) I now get to pick a new template:
And when coding a property I use proppc:
I am quite happy with this. There are other concerns such as:
- When updating the property a lot (animation) this code generates a lot of EventArgs objects littering the heap.
- The check for the correct name is done only in the Debug build.
Thoughts and solutions can be found here:
Copy the snippet to: <drive>:Users<user>DocumentsVisual Studio 2010Code SnippetsVisual C#My Code Snippets
Copy the zipped template to: <drive>:Users<user>DocumentsVisual Studio 2010TemplatesItemTemplatesVisual C#
NB: Please also read my next post