How to: Initialize Business Objects with Default Property Values in XPO 如何:在 XPO 中用默认属性值初始化业务对象
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 事件处理程序中执行此操作。
How to: Initialize Business Objects with Default Property Values in XPO 如何:在 XPO 中用默认属性值初始化业务对象的更多相关文章
- How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象
When designing business classes, a common task is to ensure that a newly created business object is ...
- Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
This lesson explains how to set rules for business classes and their properties. These rules are val ...
- QML的默认属性default property
qml中,普通的属性,需要添加属性名称,属性内容,如 color: “red” 默认属性则可以直接书写,去掉方括号,在写重用的QML组件式比较有用,例如将一个QmL外部资源封装好,内部具体的item, ...
- Default Parameter Values in Python
Python’s handling of default parameter values is one of a few things that tends to trip up most new ...
- How to: Calculate a Property Value Based on Values from a Detail Collection 如何:基于详细信息集合中的值计算属性值
This topic describes how to implement a business class, so that one of its properties is calculated ...
- Format a Property Value 设置属性值的格式
In this lesson, you will learn how to set a display format and an edit mask to a business class prop ...
- Implement Property Value Validation in the Application Model 在应用程序模型中实现属性值验证
In this lesson, you will learn how to check whether or not a property value satisfies a particular r ...
- iOS @property的默认属性
我是一个比较懒的人,很多情况下@property都不喜欢加属性…所以必须了解默认情况下哪些是已经有的,哪些在需要时是必须要加的. 原文链接:http://blog.sina.com.cn/s/blog ...
- 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values
对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...
随机推荐
- js杂项积累
主要内容: 一 浏览器重定向Http请求跨域 二 html select标签 可以设置属性multipe,变为多选 三 document.wirte只应在script标签的顶层代码中使用.不能放在函数 ...
- 用IDEA详解Spring中的IoC和DI(挺透彻的,点进来看看吧)
用IDEA详解Spring中的IoC和DI 一.Spring IoC的基本概念 控制反转(IoC)是一个比较抽象的概念,它主要用来消减计算机程序的耦合问题,是Spring框架的核心.依赖注入(DI)是 ...
- FSM有限状态机 ---C#、Unity
抽象类State public interface State//定义状态接口 { void Init();//初始化 int GetCurrentStateId();//返回当前状态Id void ...
- .Net core_Excel 导出二维码(以导出箱单为例)
[AccessLogAttribute(Note = "导出条形码箱单 — 条形码")]public ActionResult ExportContract(string INNE ...
- CollectionView常用的布局方式总结
结合网上的collectionView常用布局整合的collectionView使用工具 下载地址 支持五种布局方式ZFCollectionViewLayoutType有两种ZFCollectionV ...
- openstack网络(四)-虚机流量分析
几种网络名词解释 使用LinuxBridge时虚机流量分析 VLAN FLAT Local VXLAN 使用OVS时虚机流量分析 几种网络名词解释 1.local网络:local网络是与其他网络和节点 ...
- JDBC技术对数据库进行操作
什么是 JDBC: • JDBC(Java DataBase Connectivity)java 数据库连接 • 是 JavaEE 平台下的技术规范 • 定义了在 Java 语言中连接数据,执行 SQ ...
- Calamari 安装
在CentOS 7 安装Calamari 2016年04月17日 18:59:06 lizhongwen1987 阅读数 8055更多 分类专栏: Ceph 版权声明:本文为博主原创文章,遵循CC ...
- 从头学pytorch(二) 自动求梯度
PyTorch提供的autograd包能够根据输⼊和前向传播过程⾃动构建计算图,并执⾏反向传播. Tensor Tensor的几个重要属性或方法 .requires_grad 设为true的话,ten ...
- 深入理解 Java 泛型