Make a Property Calculable that the properties calculated

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.

In this lesson, you will learn how to calculate property management. For this reason, payment will achieve class. Attribute value for an amount calculated using the "speed" and "h" attribute. After changing the "rate" attribute, that value will be updated immediately.

 

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

 

  • Inherit from the Business Class Library Class (XPO/EF)
  • (XPO / EF) business class inherits from the class library
  • Place an Action in a Different Location
  • The operation in other locations
  • 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.

  • To achieve the payment category, right-click MySolution module project "business objects" folder, then select "Add DevExpress project" |. New project ... in the template library calls, select XAF Business Objects | If you are using XPO and XAF business object, the business object templates XPO | If ORM is EF, the EF business object template, enter "payment" as the file name, then click "Add." The automatically generated class declaration with the following code.

 

         eXpress Persistent Objects

         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(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 {
            object tempObject = EvaluateAlias(nameof(Amount));
            if(tempObject != null) {
                return (double)tempObject;
            }
            else {
                return 0;
            }
        }
    }
}

 

Entity Framework

          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
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.
Note that if you use the Entity Framework, you must register a new class in the DbContext. For more information, please see the library inherited from the business class (EF).
The Amount property is calculated, as it has no set accessor, and the logic of its value calculation is implemented in the get accessor.
Calculation "Amount" attribute, because it does not set the access device, and the logic value thereof is calculated to achieve the get accessor.

 

Note 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.
In the above code, "persistence attribute alias" modification "calculated number of non-persistent attributes to allow execution of filtering and sorting of this property at the database level." Persistent Alias ​​"attribute takes a single parameter that specifies a database for calculating expression server property values ​​must be specified in the code as a parameter persistent alias attribute. However, in some cases, this property may require sustained alias can be configured, deployed and must be configured by the administrator of the application in this case, an alias should be calculated using the persistent properties.

 

  • 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.

  • Rebuild MySolution. Module and call its project model editor. Navigate to BOModel | Payment | own members | rate node. The "immediate posting data rate" property is set to True. "Data released immediately" attribute specifies changed after the current bound control property editor, the attribute value is updated immediately. Since the calculation of the "amount" attribute value depends on the "speed", these values ​​will be updated simultaneously in the UI.

    Tutorial_UIC_Lesson6_1

    Note Note
    Alternatively, you can use the ImmediatePostDataAttribute in code.
    Alternatively, you can use the "immediate release data attributes" in the 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.

  • Run WinForms applications. Choose a payment item in the navigation control. Click the "New" button. For more information call the new form of payment object. Designated "rate" and "hour" attribute, and save your changes. Then, change the "rate" and "hour" attribute, and see how this affects the "amount" attribute. And "Hour" property field after losing focus, "amount" attribute value is updated immediately when changes to "rate" attribute value.

    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.

  • 运行ASP.NET应用程序。在导航控件中选择付款项目。在编辑模式下调用付款详细信息视图。然后,更改"速率"和"小时"属性,以查看这如何影响"金额"属性。在"速率"属性字段失去焦点后,该页将立即更新。如果更改"小时"属性值,则在保存更改后将更新"金额"值。

 

 

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 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/

You can see the changes in the main lesson presentation | main demonstration module project. The demo application is installed in the main % the PUBLIC% \ Documents \ DevExpress the Demos 19.2 \ Components \ eXpressApp Framework \ MainDemo by default. The Available Online at The ASP.NET Version IS AT http://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.

Please note that in MainDemo, the "hour" of the "immediate release data" attribute is also set to "True", and therefore behavior and the behavior described in this tutorial different.

 

Guess you like

Origin www.cnblogs.com/foreachlife/p/Make-a-Property-Calculable.html