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. shell脚本编程基础--文本比较

    1.概述 允许测试Linux文件系统上文件的目录和状态. 2.详解 2.1 检查目录 -d测试会检查指定的目录是否存在于系统中.当我们打算将文件写入目录或是准备切换到该目录时,先进行测试是比较好的做法 ...

  2. mysql5.7 gruop by报错this is incompatible with sql_mode=only_full_group_by

    解析:在mysql 工具 搜索或者插入数据时报下面错误: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY cla ...

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

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

  4. [TimLinux] Python 迭代器(iterator)和生成器(generator)

    1. 可迭代对象 from collection import Iterable class Iterable(metaclass=ABCMeta): ... def __iter__(self): ...

  5. sql service 从创建访问用户到数据库访问 【SQL】

    create login [LoginMame] with password=[Pwd]--创建数据库登陆用户 create user [DBLoginName] for login [LoginMa ...

  6. Python发送邮件以及对其封装

    对Python发送邮件进行封装 Python发送邮件分为四步 连接到smtp服务器 登陆smtp服务器 准备邮件 发送邮件 导入所需要的包 import smtplib from email.mime ...

  7. 【CSS】346- 你所不知道的 CSS 阴影技巧与细节

    偷懒了1个多礼拜,在工作饱和的情况下,怎么坚持学习?今天的分享来自@Coco国服第一切图仔,我们聊聊CSS属性box-shadow- 关于 CSS 阴影,之前已经有写过一篇,box-shadow 与 ...

  8. 浅析Java堆,栈,方法区

    栈(Stack) 1.栈是线程私有的,其生命周期和线程相同. 2.每个方法在执行的时候都会开辟一个栈区,同时创建一个栈帧(Stack Frame). 3.栈帧用于存储局部变量表,操作数栈,动态链接和方 ...

  9. 小胖求学系列之-文档生成利器(下)-smart-doc

    叮叮叮....一阵铃声响起,大家都陆续来到了课堂,看老师没来,小张和小胖又闲聊起来,小张问:怎么样,smart-doc好用吧.小胖笑着说:挺好用的,不过? 小张看卖关子,问到:不过什么,有什么新发现? ...

  10. Docker下载tomcat

    命令 下载tomcat docker pull tomcat //默认是latest版本具体可以到 hub.docker.com上查询 //如果想下其他版本以9.0.16示例那么: docker pu ...