Skip to main content
A newer version of this page is available. .

Simplified Property Syntax

  • 2 minutes to read

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(nameof(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(nameof(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>(nameof(OrderDate)); }
    set { SetPropertyValue(nameof(OrderDate), value); }
}
See Also