When working with Silverlight two-way binding (and not only there) you need to implement the INotifyPropertyChanged interface in order to notify any observers that the property values of the object have changed.
The INotifyChangedProperty exposes only the PropertyChanged event which is used by observers to subscribe to the change notifications. The typical pattern is to have a base class which will fire the events like this:
This can be used like this:
For small projects, this implementation is ok, but on large scale projects, using such an implementation will lead to a maintenance nightmare, especially when performing property rename refactorings. After each refactoring, you need to manually change the name of the property inside the setter.
Type safe implementation using Expression Trees
.NET 3.5 comes with the ability of navigating through the object hierarchy using Expression trees. This is used for example in LINQ providers for dynamically building the SQL queries. The same Expression trees can be used in this case for building a type safe solution for the OnPropertyChanged method:
Coming back to the class example above, the new type safe implementation can be used as below:
In this blog article I have presented a way of implementing a type-safe notifier for the INotifyPropertyChanged interface. The implementation presented here uses expression trees for achieving this.
I haven’t tested the performance overhead of using expression trees, but I guess if this is an issue, you can easily cache the results for further usage.