Make a Property Calculable 使属性可计算
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 使属性可计算的更多相关文章
- Android动画效果之初识Property Animation(属性动画)
		
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
 - Objective-C中变量采用@property的各个属性值的含义
		
我们在OC中定义变量,可以自己来定义变量的setter方法来设置变量值,用getter方法来获取变量值.但是当变量数量增多时,还采用手动添加setter/getter方法来操作变量,就会使得程序代码量 ...
 - Swift编程语言学习9—— 存储属性和计算属性
		
属性将值跟特定的类.结构或枚举关联.存储属性存储常量或变量作为实例的一部分,计算属性计算(而不是存储)一个值.计算属性能够用于类.结构体和枚举里,存储属性仅仅能用于类和结构体. 存储属性和计算属性通经 ...
 - @property括号内属性讲解
		
一.前言 一个object的属性允许其他object监督和改变他的状态.但是在一个设计良好的面向对象程序中,直接访问一个object的内部状态是不可能的.相反,存取器(getter sett ...
 - 第7.23节 Python使用property函数定义属性简化属性访问的代码实现
		
第7.23节 Python使用property函数定义属性简化属性访问的代码实现 一. 背景 在本章前面章节中,我们介绍了类相关的知识,并举例进行了说明,在这些例子中会定义一些形如 ...
 - OC中property的有关属性
		
property的有关属性: (1)readwrite是可读可写特征:需要生成getter方法和setter方法: (2)readonly是只读特性只会生成getter方法不会生成setter方法: ...
 - 关于@property()的那些属性及ARC简介
		
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
 - Swift面向对象基础(中)——Swift中的存储属性和计算属性
		
学习来自<极客学院> 1.存储属性:存储在类.结构体里的变量或者常量 2.分为:实例存储属性.类型存储属性 3.所有的存储属性必须显示的指定初始值,在定义时或者构造器当中指定 4.可选类型 ...
 - 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】
		
@property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...
 
随机推荐
- 为什么现在使用多周期CPU,而单周期CPU被弃用?
			
最初设计的CPU结构简单,内部不复杂.之所以制造它是为了让机器自动跑程序,算数. 早期CPU都是单周期的,人们没考虑那么多,性能啥的.就让CPU每个时钟周期跑一个指令,这些时钟周期等长.这样下来,有的 ...
 - 这个七夕节,用Python为女友绘制一张爱心照片墙吧!【华为云技术分享】
			
欢迎添加华为云小助手微信(微信号:HWCloud002 或 HWCloud003),输入关键字“加群”,加入华为云线上技术讨论群:输入关键字“最新活动”,获取华为云最新特惠促销.华为云诸多技术大咖.特 ...
 - 华为云BigData Pro解读: 鲲鹏云容器助力大数据破茧成蝶
			
华为云鲲鹏云容器 见证BigData Pro蝶变之旅大数据之路顺应人类科技的进步而诞生,一直顺风顺水,不到20年时间,已渗透到社会生产和人们生活的方方面面,.然而,伴随着信息量的指数级增长,大数据也开 ...
 - Go游戏服务端框架从零搭建(一)— 架构设计
			
五邑隐侠,本名关健昌,10年游戏生涯,现隐居海边. 本教程以Go语言分区游戏服务端框架搭建为例. Go语言是Google开发的一种静态强类型.编译型.并发型.具有垃圾回收功能的编程语言.语法上近似C语 ...
 - PXE+Kickstart网络装机(Centos6.5版本)
			
1.原理说明: PXE Client:表示需要安装操作系统的机器,统称客户端: TFTP server:表示安装TFTPD服务的机器: DHCP server:表示安装DCHPD服务的机器: 在实际的 ...
 - idea结合maven小项目
			
整体构造 (修改 POM 文件 )parent <?xml version="1.0" encoding="UTF-8"?> <project ...
 - MyBatis_多表关联查询_resultMap_单个对象_N+1方式实现
			
mapper 层 提供 StudentMapper 和 ClazzMapper, StudentMapper 查询所有学生信息, ClazzMapper 根据编号查询班级信息. 再 StudentMa ...
 - CSU-2018
			
The gaming company Sandstorm is developing an online two player game. You have been asked to impleme ...
 - 使用stm32开发  USB_CAN 适配器测试
			
USB_CAN 适配器测试例程 采用CDC透传模式 一.简介 CAN总线无处不在,在设计开发中,到处需要用到CAN总线调试工具,本工具可以作为CAN的基础测试工具,用于监听CAN总线,或测试CAN数据 ...
 - electron中JS报错:require is not defined的问题解决方法
			
Electron已经发布了6.0正式版,升级后发现原来能运行的代码报错提示require is not defined 解决办法: 修改创建BrowserWindow部分的相关代码,设置属性webPr ...