Property System
Property System
WPF has a really nice feature called Dependency Property, from the user perspective a dependency property is a standard CLR property that add, on top of CRL properties, a set of really nice and powerful features:
Property value inheritance;
Property metadata;
Property change notification;
Support for default value generation;
…and many others strictly related to WPF;
The Radical assembly where the property system lives is totally non-related to WPF in any way, we have simply decided to bring the power of dependency-like properties in order to give some interesting boost to certain part of the Radical framework.
One really interesting thing of the dependency properties, the WPF ones, is that values and metadata are stored at the root object level, we inherited that concept in our Entity
base abstract class; lots of Radical stuff inherit from the Entity
base class so to obtain something really interesting:
what we see here is what we call a Radical property (RP), that from the outside is viewed, and behaves, like a standard CLR property but, from the inside, is totally managed by the Entity
base class, and in our object we only expose a property.
Property change notification
The first thing we get using a Radical property is property change notification, the base Entity
class implements INotifyPropertyChanged
and automatically fires the event whenever the property really changes; really means that subsequently setting the same value more than once fires the event only the first time.
Property Metadata
Since we have everything managed by the base class, thus the base class holds all the properties and property values we can easily introduce the concept of csharp
attached to a property without requiring the inheriting class to do nothing:
We are retrieving the default property metadata for the given property, using metadata the first thing we can do is to define the property default value.
Default Value
The property default value is requested the first time a property get is issued, we will use the property metadata to define the default value for a property because we do not want to trigger a PropertyChanged event for the simple fact of defining a default, initial value:
but much more interesting is the possibility to intercept the default value request using a lambda:
so to be able to perform some logic when the default value is requested. Both approaches can be used in a fluent manner:
Cascade changes
once we have property metadata we can add some interesting features such as cascade change notifications:
in this sample each time the MyProperty changes the PropertyChanged event is raised even for the AnotherProperty property. The RemoveCascadeChangeNotifications can be used to remove a cascade change notification previously added.
Disable change notifications
by default all the radical properties notify of their change, if we want to disable change notifications for a specific property we’ll use once again property metadata:
at a later time changes can be re-enabled using the EnableChangeNotifications method.
Change detection
In the case we need to detect the change of a property from within the object itself we can use property metadata:
or directly interact with the property definition:
in both cases we get access to the current property value and to old property value.
Last updated