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

Make a Property Calculable

  • 5 minutes to read

In this lesson, you will learn how to manage calculated properties. For this purpose, the Payment class will be implemented. Its Amount property value will be calculated using the Rate and Hours properties. The value will be updated immediately after changing the Rate property.

Note

Before proceeding, take a moment to review the following lessons.

  • To implement the Payment class, right-click the Business Objects folder in the MySolution.Module project and choose Add DevExpress Item | New Item…. In the invoked Template Gallery, select the XAF Business Object | XPO Business Object template if you use XPO and XAF Business Object | EF Business Object template if your ORM is EF, enter “Payment” as the file name, and click Add. Replace the automatically generated class declaration with the following code.

    eXpress Persistent Objects

    [DefaultClassOptions, ImageName("BO_SaleItem")]
    public class Payment : BaseObject {
        public Payment(Session session) : base(session) { }
        private double rate;
        public double Rate {
            get {
                return rate;
            }
            set {
                if(SetPropertyValue("Rate", ref rate, value))
                    OnChanged("Amount");
            }
        }
        private double hours;
        public double Hours {
            get {
                return hours;
            }
            set {
                if(SetPropertyValue("Hours", ref hours, value))
                    OnChanged("Amount");
            }
        }
        [PersistentAlias("Rate * Hours")]
        public double Amount {
            get {
                object tempObject = EvaluateAlias("Amount");
                if(tempObject != null) {
                    return (double)tempObject;
                }
                else {
                    return 0;
                }
            }
        }
    }
    

    Entity Framework

    using System;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations.Schema;
    using DevExpress.Persistent.Base;
    
    namespace MySolution.Module.BusinessObjects {
        [DefaultClassOptions, ImageName("BO_SaleItem")]
        public class Payment {
            [Browsable(false)]
            public Int32 ID { get; protected set; }
            public double Rate { get; set; }
            public double Hours { get; set; }
    
            [NotMapped]
            public double Amount {
                get { return Rate * Hours; }
            }
        }
    }
    

    Note

    Note that if you use the Entity Framework, you have to register the new class in DbContext. See Inherit from the Business Class Library Class (EF) for details.

    The Amount property is calculated, as it has no set accessor, and the logic of its value calculation is implemented in the get accessor.

    Note

    In the code above, the Amount non-persistent calculated property is decorated with the PersistentAliasAttribute, to allow filtering and sorting by this property to be performed 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. However, in certain scenarios, the property may require a configurable persistent alias, and it must be configurable by an administrator in a deployed application. In this case, the CalculatedPersistentAliasAttribute should be used.

  • Rebuild the MySolution.Module project and invoke the Model Editor for it. Navigate to the BOModel | Payment | OwnMembers | Rate node. Set the Rate‘s ImmediatePostData property to True. The ImmediatePostData property specifies whether or not the property value is updated immediately after changes occur in the current Property Editor’s bound control. As the calculated Amount property value depends on Rate, these values will be updated simultaneously in the UI.

    Tutorial_UIC_Lesson6_1

    Note

    Alternatively, you can use the ImmediatePostDataAttribute in code.

  • Run the WinForms application. Select the Payment item in the navigation control. Click the New button. The detail form for the new Payment object will be invoked. Specify the Rate and Hours properties, and save the changes. Then, change the Rate and Hours properties, and see how this affects the Amount property. The Amount property value is updated immediately when changing the Rate property value, and also after the Hours property field loses focus.

    Tutorial_UIC_Lesson6_2

  • Run the ASP.NET application. Select the Payment item in the navigation control. Invoke the Payment Detail View in edit mode. Then, change the Rate and Hours properties to see how this affects the Amount property. The page is updated immediately after the Rate property field loses focus. If you change the Hours property value, the Amount value is updated after saving the changes.

    Tutorial_UIC_Lesson6_3

You can see the changes made in this lesson in the Main Demo | MainDemo.Module project. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 18.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version of this demo is available online at https://demos.devexpress.com/XAF/MainDemo. Note that in MainDemo, the ImmediatePostData property of Hours is also set to “True“, so the behavior is different from the behavior described in this tutorial.

 

Next Lesson: Filter Lookup Editor Data Source

See Also