When designing business classes, a common task is to ensure that a newly created business object is initialized with default property values. This topic explains how different types of properties can be initialized. As an example, a Contact business class will be implemented. After a Contact object is created, its properties will be initialized with default values.

设计业务类时,常见的任务是确保使用默认属性值初始化新创建的业务对象。本主题说明如何初始化不同类型的属性。例如,将实现联系人业务类。创建"联系人"对象后,其属性将用默认值初始化。

Tip 提示
A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E2053
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=E2053

.

Tip 提示
A similar example for Entity Framework is available in the How to: Initialize Business Objects with Default Property Values in Entity Framework topic.
实体框架的类似示例在"如何:在实体框架中使用默认属性值初始化业务对象"主题中可用。

Simple Property

简单属性

All the base persistent classes are derived from the PersistentBase class. This class exposes the PersistentBase.AfterConstruction method intended for object initialization. The AfterConstruction method is called only once for an object - after the object is created. Whenever you need to initialize an object, you should override these methods and place the initialization code into its body. As this method is specifically designed for initialization, there is no need to check the current object state when assigning values to the object properties. The following code snippet demonstrates how simple value properties can be initialized.

所有基持久性类都派生自持久库类。此类公开用于对象初始化的持久基础.后构造方法。在创建对象后,仅对对象调用一次 After 构造方法。每当需要初始化对象时,应重写这些方法并将初始化代码放入其正文中。由于此方法专为初始化而设计,因此在将值分配给对象属性时无需检查当前对象状态。以下代码段演示如何初始化简单值属性。

public class Contact : Person {
//...
public override void AfterConstruction() {
base.AfterConstruction(); FirstName = "Sam";
TitleOfCourtesy = TitleOfCourtesy.Mr;
}
}

To see another example of initializing a simple property, refer to the Initialize a Property After Creating an Object (XPO) tutorial lesson.

要查看初始化简单属性的另一个示例,请参阅创建对象 (XPO) 教程课后初始化属性。

Reference Property

引用属性

Initialization of reference properties differs from initialization of simple properties, primarily in that you may need to obtain a reference to an existing object. For this purpose, use the Session.FindObject method of the object's Session. The following code snippet demonstrates how to initialize reference properties with new and existing objects.

引用属性的初始化不同于简单属性的初始化,主要是因为您可能需要获取对现有对象的引用。为此,请使用对象的会话的会话.FindObject 方法。以下代码段演示如何使用新对象和现有对象初始化引用属性。

public class Contact : Person {
//...
public override void AfterConstruction() {
base.AfterConstruction();
Address1 = new Address(Session);
Address1.Country = Session.FindObject<Country>(CriteriaOperator.Parse("Name = 'USA'"));
if(Address1.Country == null) {
Address1.Country = new Country(Session);
Address1.Country.Name = "USA";
Address1.Country.Save();
}
Manager = Session.FindObject<Contact>(CriteriaOperator.Parse(
"FirstName = 'John' && LastName = 'Doe'"));
}
}

Collection Property

集合属性

To populate business object collections, use the XPCollection.Add method. The following code snippet demonstrates how to populate the Phones collection with predefined phone numbers.

要填充业务对象集合,请使用 XPCollection.Add 方法。以下代码段演示如何使用预定义的电话号码填充电话集合。

public class Contact : Person {
//...
public override void AfterConstruction() {
base.AfterConstruction(); PhoneNumber phone1 = Session.FindObject<PhoneNumber>(CriteriaOperator.Parse(
"Number = '555-0101'"));
PhoneNumber phone2 = Session.FindObject<PhoneNumber>(CriteriaOperator.Parse(
"Number = '555-0102'"));
PhoneNumbers.Add(phone1);
PhoneNumbers.Add(phone2);
}
}

Calculated Property

计算属性

A calculated property value is automatically updated when the associated property values are changed. To learn how to implement a regular calculated property, refer to the Make a Property Calculable tutorial lesson. To learn how to implement a calculated property based on property values of the objects contained in a child object collection, refer to the How to: Calculate a Property Value Based on Values from a Detail Collection help topic.

更改关联的属性值时,将自动更新计算的属性值。要了解如何实现常规计算属性,请参阅创建属性可计算教程课。要了解如何实现基于子对象集合中包含的对象的属性值的计算属性,请参阅"如何:基于详细信息集合帮助中的值计算属性值"。

Initialize an Object Created via the New Action

初始化通过新操作创建的对象

In certain scenarios, you may need to initialize only objects created specifically via the New Action. To learn how to do this, refer to the How to: Initialize an Object Created Using the New Action help topic.

在某些情况下,您可能需要仅初始化通过"新建操作"专门创建的对象。要了解如何执行此操作,请参阅"如何:初始化使用"新建操作"帮助创建的对象主题。

Initialize a Property of a Child Object with a Value Taken from Master Object

初始化子对象的属性,具有从主对象获取的值

You can set the default value for the child object's property within the setter of a property that refers to the master object.

您可以在引用主对象的属性的 setter 中设置子对象属性的默认值。

public class ChildObject : BaseObject {
// ...
public MasterObject MasterObject {
get { return masterObject; }
set {
bool modified = SetPropertyValue(nameof(MasterObject), ref masterObject, value) ;
if (!IsLoading && !IsSaving && value != null && modified) {
this.SomeProperty = value.DefaultForChildren;
}
}
}
}

It is impossible to obtain parent object values in a child object's AfterConstruction method because this method is called before any properties are initialized from an outside code. If you need to execute some code according to the assigned parent object, do this either in the ChildObject.MasterObject property setter or in the XPBaseCollection.CollectionChanged event handler.

在子对象的 After 构造方法中无法获得父对象值是不可能的,因为在从外部代码初始化任何属性之前调用此方法。如果需要根据分配的父对象执行某些代码,请在 ChildObject.MasterObject 属性集码器或 XPBaseCollection.Collection 事件处理程序中执行此操作。

最新文章

  1. 【目录】JUC集合框架目录
  2. 『.NET Core CLI工具文档』(六)dotnet 命令
  3. kuangbin_ShortPath R (HDU 4370)
  4. CSS浮动讲解好文章推荐
  5. TL-WR703 USB不稳定/当前的总结
  6. js实现按回车自行提交
  7. Delphi NativeXML 乱码的问题
  8. jQuery validate和form插件配套使用
  9. dotnetcore vue+elementUI 前后端分离架二(后端篇)
  10. eval及json的理解
  11. leetcode 388.Lonest Absolute File Path
  12. 如何启动Intel VT-X及合理利用搜索
  13. Rop框架学习笔记
  14. JSTL标签用法:&lt;c:choose&gt;&lt;c:forEach&gt;&lt;c:if&gt;&lt;c:when&gt;&lt;c:set&gt;
  15. LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
  16. 洛谷P2194HXY烧情侣
  17. Docker 私有仓库最简便的搭建方法
  18. (转)js获取内网ip地址,操作系统,浏览器版本等信息
  19. 《Spring实战》-2
  20. SecureCRT 遇到一个致命的错误且必须关闭

热门文章

  1. 基于vue的cropper插件编写分享
  2. #华为云·寻找黑马程序员# 如何实现一个优雅的Python的Json序列化库
  3. 转:Eclipse中创建Maven版的Web工程(详解)
  4. 强化学习一:Introduction Of Reinforcement Learning
  5. HDU3870-Caught these thieves(最小割-&amp;gt;偶图-&amp;gt;最短路问题)
  6. E1.Send Boxes to Alice(Easy Version)//中位数
  7. Python之数据分析工具包介绍以及安装【入门必学】
  8. django学习01-建project和app
  9. JavaScript 逻辑与(&amp;&amp;) 与 逻辑或(||) 运算规则
  10. InputStream 读取中文乱码 扩展