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 中同时更新。

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

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

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",因此行为与本教程中描述的行为不同。

Make a Property Calculable 使属性可计算的更多相关文章

  1. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  2. Objective-C中变量采用@property的各个属性值的含义

    我们在OC中定义变量,可以自己来定义变量的setter方法来设置变量值,用getter方法来获取变量值.但是当变量数量增多时,还采用手动添加setter/getter方法来操作变量,就会使得程序代码量 ...

  3. Swift编程语言学习9—— 存储属性和计算属性

    属性将值跟特定的类.结构或枚举关联.存储属性存储常量或变量作为实例的一部分,计算属性计算(而不是存储)一个值.计算属性能够用于类.结构体和枚举里,存储属性仅仅能用于类和结构体. 存储属性和计算属性通经 ...

  4. @property括号内属性讲解

    一.前言      一个object的属性允许其他object监督和改变他的状态.但是在一个设计良好的面向对象程序中,直接访问一个object的内部状态是不可能的.相反,存取器(getter sett ...

  5. 第7.23节 Python使用property函数定义属性简化属性访问的代码实现

    第7.23节 Python使用property函数定义属性简化属性访问的代码实现 一.    背景       在本章前面章节中,我们介绍了类相关的知识,并举例进行了说明,在这些例子中会定义一些形如 ...

  6. OC中property的有关属性

    property的有关属性: (1)readwrite是可读可写特征:需要生成getter方法和setter方法: (2)readonly是只读特性只会生成getter方法不会生成setter方法: ...

  7. 关于@property()的那些属性及ARC简介

    @property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...

  8. Swift面向对象基础(中)——Swift中的存储属性和计算属性

    学习来自<极客学院> 1.存储属性:存储在类.结构体里的变量或者常量 2.分为:实例存储属性.类型存储属性 3.所有的存储属性必须显示的指定初始值,在定义时或者构造器当中指定 4.可选类型 ...

  9. 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】

    @property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...

随机推荐

  1. 洛谷 题解 P5535 【【XR-3】小道消息】

    我又双叒叕被包菜辣! P5535 [XR-3]小道消息(这道题是个大水题 在题干中这位良心的作者就提醒了我们: 你可能需要用到的定理--伯特兰-切比雪夫定理. 那么什么是伯特兰-切比雪夫定理? 我也不 ...

  2. 移动开发在路上-- IOS移动开发系列 网络交互四(2)

    接着上次的讲,这次我们讲 网络请求的封装  打开创建的项目,让我们一起来继续完成他, 首先我们来创建一个NSobject 的文件 圈住出来的轻一点要注意.千万不要搞错了 创建好之后,开始编写代码, 我 ...

  3. openlayers4 入门开发系列结合 echarts4 实现交通线流动图

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  4. 数理统计(一)——用Python进行方差分析

    数理统计(一)——Python进行方差分析 iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 方差分析可以用来推断一个或多个因素在其状态变化时,其因素水平或交互 ...

  5. 使用ExcelPackage进行Excel报表

    Nuget包名为 epplus.core 命名空间OfficeOpenXml string localFileName = path + Path.DirectorySeparatorChar + f ...

  6. CMFCPropertyGridCtrl的属性改变事件代码

    //用于区分Prop, 使用SetData, GetData方法 CMFCPropertyGridProperty* pProp1 = new CMFCPropertyGridProperty(str ...

  7. Linux-tac、diff、tree、echo、seq、重定向

    1.tac  方向输出文件,最后一行放在第一行的位置输出 2.diff  比较文件的内容 vimdiff:在vim中比较 3. tree  树状图显示目录内容 -d 只显示目录   -L  树状 目录 ...

  8. robotframework配置邮箱服务器

    1.登录邮箱以腾讯企业邮箱为例:开启smtp服务并获得邮箱的客户端授权码 用户名:18890260218@163.com 客户端授权码:admin123 2.进入系统管理-->GO to plu ...

  9. 【hibernate】存储图片

    [hibernate]存储图片 转载: package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import j ...

  10. 使用python实现数组、链表、队列、栈

    引言 什么是数据结构? 数据结构是指相互之间存在着一种或多种关系的数据元素的集合和该集合中数据元素之间的关系组成. 简单来说,数据结构就是设计数据以何种方式组织并存储在计算机中. 比如:列表,集合和字 ...