The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

Simplified Property Syntax

Simplified Property Setters provide an easy way to implement persistent properties. The example below shows a standard persistent property implementation:


DateTime fOrderDate;
public DateTime OrderDate {
    get { return fOrderDate; }
    set {
        if(fOrderDate != value) {
            DateTime OldValue = fOrderDate;
            fOrderDate = value;
            OnChanged("OrderDate", OldValue, fOrderDate);
        }
    }
}

The 6.3.1 version of XPO introduces two new PersistentBase class methods that simplify the implementation. Namely, the SetPropertyValue and GetPropertyValue methods.

The SetPropertyValue method is designed to shorten the typical set accessor declaration. The method takes three parameters: the name of the property, reference to the property's private field, and the new property value. In the following code the body of the set accessor from the preceding example is replaced by a single SetPropertyValue method call.

First, the method checks whether or not the new property value is different from the old value. If it is, the old value is remembered and the property value is set to the new one. Then the property change event is triggered, for XPO to take note of the persistent object change.


DateTime fOrderDate;
public DateTime OrderDate {
    get { return fOrderDate; }
    set { SetPropertyValue("OrderDate", ref fOrderDate, value); }
}

The use of GetPropertyValue method allows you to omit the property's private field declaration. In this case, to implement the simplified set accessor, it is necessary to use the SetPropertyValue method overload that takes two parameters: the property name and the new property value. The preceding example can be further rewritten as follows:


public DateTime OrderDate {
    get { return GetPropertyValue<DateTime>("OrderDate"); }
    set { SetPropertyValue("OrderDate", value); }
}
See Also