The PDFx is a lightweight open source .NET library that allows developers to describe dependencies between Properties in declarative C# code. Once relationships are registered, the framework monitors property changes and ensures that the INotifyPropertyChanged.PropertyChanged event is fired for all directly and indirectly dependent properties in a very efficient way. The library is available for WPF, Silverlight, WinRT (Windows Store) and Windows Phone.
I’ve developed the PDFx as an InterKnowlogy RECESS project and published the source code and examples on codeplex.
In a series of blog posts I am going to cover the library’s most important features:
- Part I: Introduction
- Part II: Library Versions
- Part III: Getting Started
- Part IV: Using PDFx with an existing 3rd party framework
- Part V: Simple Property Dependencies
- Part VI: External Property Dependencies
- Part VII: Dynamic External Property Dependencies
- Part VIII: Collection Dependencies
- Part IX: Dynamic Collection Dependencies
- Part X: Caching
- Part XI: Smart Property Changed Notification
- Part XII: Callbacks
- Part XIII: Sanity Checks
- Part XIV: Data Delegation
- Part XV: Two Way Converters
- Part XVI: One Way Converters
Smart Property Changed Notification
Whenever a property changes in a property dependency network, the PDFx can optionally first analyze all the property dependencies and then fire the INotifyPropertyChanged.PropertyChanged event only once for every directly or indirectly dependent property. This analysis saves tremendous amounts of precious CPU time since every PropertyChanged event causes your UI layer to access and thereby reevaluate the bound properties.
By default, the smart property changed notification is turned on.
If you have a reason to switch this behavior off, you can set the Bindable class’s UseSmartPropertyChangedNotificationByDefault Property to false.
The source code of this example can be found in ViewModel SmartPropertyDependencyVM which is part of the WPFSample’s source code.
Let us look at the following object graph:
Green circles stand for Input Properties while purple circles indicate calculated properties. The arrows show the underlying math operations as well as the property dependencies.
The number next to the lightning icon indicates how often the INotifyPropertyChanged.PropertyChanged event has fired since the D1 Property has changed the last time.
A change of D1 results in the following:
As the picture indicates, INotifyPropertyChanged.PropertyChanged is fired only once for every single property for maximum performance.
When the Smart Property Change Notification is switched off, a change of D1 results in the following:
In this scenario, the PDFx does not analyze the Property Dependency Network before INotifyPropertyChanged.PropertyChanged events are fired but rather blindly performs a depth-search traversal down the dependency tree along all directly and indirectly dependent properties and fires INotifyPropertyChanged.PropertyChanged for every single property it finds. As shown above, this behavior results in 10 PropertyChanged Notifications for the root node, A1.
To be more specific, the PDFx traverses the tree in the following manner, if smart property change notification is switched off:
D1 -> C1 -> B1 -> A1 -> B2 -> A1 -> C2 -> B2 -> A1 -> B3 -> A1 -> C3 -> B3 -> A1 -> B4 -> A1 -> C4 -> B4 -> A1 -> B5 -> A1 -> C5 -> B5 -> A1 -> B6 -> A1
It becomes obvious that the PropertyChanged event is inefficiently raised 10 times for Property A1 although once would be completely sufficient. Consequently, it is advisable to always take advantage of the smart property notification.