Skip to main content
A newer version of this page is available. .
All docs
V20.2

Create a Calculated Property

  • 2 minutes to read

This lesson explains how to create calculated properties.

In this lesson, you will add a Payment class with the following properties:

  • Rate (a persistent property)
  • Hours (a persistent property)
  • Amount (a non-persistent, calculated property: Amount = Rate * Hours)

Note

Before you proceed, take a moment to review the following lessons.

Step-by-Step Instructions

  1. Implement the Payment class: right-click the Business Objects folder in the MySolution.Module project and choose Add Item | Class, enter “Payment” as a file name, and click Add.
  2. Replace the generated class declaration with the following code:

    [DefaultClassOptions]
    public class Payment : BaseObject {
        public Payment(Session session) : base(session) { }
        private double rate;
        public double Rate {
            get {
                return rate;
            }
            set {
                if(SetPropertyValue(nameof(Rate), ref rate, value))
                    OnChanged(nameof(Amount));
            }
        }
        private double hours;
        public double Hours {
            get {
                return hours;
            }
            set {
                if(SetPropertyValue(nameof(Hours), ref hours, value))
                    OnChanged(nameof(Amount));
            }
        }
        [PersistentAlias("Rate * Hours")]
        public double Amount {
            get { return (double)(EvaluateAlias(nameof(Amount)) ?? 0); }
        }
    }
    
  3. Run the application. Select the Payment item in the navigation control, and click “New”. In the Payment Detail View, change the Rate and Hours properties to see how this affects the Amount property.

    The Amount property is updated after the Rate and Hours property fields lose focus.

    xaf blazor calculable property

Detailed Explanation

The Amount property has no set accessor - its value calculation logic is implemented in the get accessor.

The non-persistent Amount calculated property is decorated with PersistentAliasAttribute. This attribute allows you to filter and sort by a calculated property at the database level.

The PersistentAlias attribute takes a single parameter that specifies the expression used to calculate the property value on the database server side. A persistent alias must be specified in code as the attribute’s parameter.

Next Lesson

Filter Lookup Editor Data Source

See Also