Make a Property Calculable 使属性可计算

时间:2022-01-01 21:43:18

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.
在继续之前,请花点时间复习以下课程。
  • Inherit from the Business Class Library Class (XPO/EF)
  • 从商务舱库类 (XPO/EF) 继承
  • Place an Action in a Different Location
  • 将操作放置在其他位置
  • 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.

  • 要实现付款类,请右键单击 MySolution.模块项目中的"业务对象"文件夹,然后选择"添加 DevExpress 项目" |新项目...在调用的模板库中,选择 XAF 业务对象 |如果使用 XPO 和 XAF 业务对象,则 XPO 业务对象模板 |如果 ORM 是 EF,则 EF 业务对象模板,输入"付款"作为文件名,然后单击"添加"。将自动生成的类声明替换为以下代码。

         eXpress Persistent Objects

eXpress 持久对象

[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 ;
}
}
}
}

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.
请注意,如果使用实体框架,您必须在 DbContext 中注册新类。有关详细信息,请参阅从业务类库类 (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.
计算"金额"属性,因为它没有设置访问器,并且其值计算的逻辑在 get 访问器中实现。
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.

  • 重新生成 MySolution.模块项目并调用其模型编辑器。导航到 BOModel |付款 |自己的会员 |速率节点。将"速率的即时过帐数据"属性设置为 True。"立即发布数据"属性指定在当前属性编辑器的绑定控件中发生更改后,属性值是否立即更新。由于计算的"金额"属性值取决于"速率",因此这些值将在 UI 中同时更新。

    Make a Property Calculable 使属性可计算

    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.

  • 运行 WinForms 应用程序。在导航控件中选择付款项目。单击"新建"按钮。将调用新付款对象的详细信息窗体。指定"速率"和"小时"属性,并保存更改。然后,更改"速率"和"小时"属性,并查看这如何影响"金额"属性。更改"速率"属性值时以及"小时"属性字段失去焦点后,"金额"属性值会立即更新。

    Make a Property Calculable 使属性可计算

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

Make a Property Calculable 使属性可计算

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/

您可以在主演示中看到本课中所做的更改 |主演示模块项目。主演示应用程序安装在%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/

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

.请注意,在 MainDemo 中,"小时"的"即时发布数据"属性也设置为"True",因此行为与本教程中描述的行为不同。