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 ...
随机推荐
- NoSql中的CAP原则
C:一致性 .A:可用性.P:分区容错性 Partition tolerance(分区容错性): 大多数分布式系统都分布在多个子网络.每个子网络就叫做一个区(partition).分区容错的意思是,区 ...
- 【原创】003 | 搭上基于SpringBoot事务思想实战专车
前言 如果这是你第二次看到师长,说明你在觊觎我的美色! 点赞+关注再看,养成习惯 没别的意思,就是需要你的窥屏^_^ 专车介绍 该趟专车是开往基于Spring Boot事务思想实战的专车,在上一篇 搭 ...
- idea中自定义快捷键
idea中自定义快捷键 在开发的过程中,总要写一些重复的代码,那么使用自定义快捷键,可以将这些经常用到的代码,自动生成. 1.在idea工具中点击File-->Settings 2.在弹出来的界 ...
- luogu P1031 均分纸牌
题目很简单,但是可以学一学贪心策略 把纸牌均匀分布,从左往右推掉不用的纸牌 #include <iostream> using namespace std; int main() { in ...
- 一款简单的C++猜数字游戏(新手必学)
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:1只小弛 废话不多说,直接上代码! #include<bits/s ...
- php 温故而知新 好久不用 又得继续学习下
1.php注释:/* */.//.#等三种方式2.echo:向浏览器输出字符串,echo其实是一个函数:返回值:无3.print:向浏览器输出字符串,它也是一个函数:返回值:整型. ...
- 使用aop切面编写日志模块
我们先自定义一个注解(一个有关自定义注解的LJ文章 https://www.cnblogs.com/guomie/p/10824973.html) /** * * 自定义日志注解 * Retentio ...
- rails 构建 API
我是来鼓吹使用 Rails 写 API 的. 原文在此: https://labs.kollegorna.se/blog/2015/04/build-an-api-now/ 原文有一个很大的缺陷就是读 ...
- 一遍文章搞清楚VO、DTO、DO、PO的概念、区别
作者:Cat Qi 概念: VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对 ...
- webpack学习2.3webpack核心概念
核心概念(四个) Entry(入口) Output(出口) Loaders()来处理其他类型的资源文件 Plugins(插件) 1.入口(Entry) 作用:代码的入口,打包的入口,单个或多个, 示例 ...