Create a Calculated Property
- 3 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:
- Inherit from the Business Class Library Class (XPO/EF core)
- Place an Action in a Different Location
Step-by-Step Instructions
- 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.
Replace the generated class declaration with the following code:
eXpress Persistent Objects
[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); } } }
Entity Framework Core
using DevExpress.Persistent.Base; using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations.Schema; using System.Runtime.CompilerServices; // ... [DefaultClassOptions, ImageName("BO_SaleItem")] public class Payment : INotifyPropertyChanged { [Browsable(false)] public Int32 ID { get; protected set; } double rate; public double Rate { get => rate; set { if (rate == value) { return; } rate = value; OnPropertyChanged(); } } double hours; public double Hours { get => hours; set { if (hours == value) { return; } hours = value; OnPropertyChanged(); } } [NotMapped] public double Amount { get { return Rate * Hours; } } private void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }
Note
If you use the EF Core, register the new class in DbContext. See Inherit from the Business Class Library Class (EF Core) for details.
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.
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