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 中用默认属性值初始化业务对象的更多相关文章

  1. 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 ...

  2. Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)

    This lesson explains how to set rules for business classes and their properties. These rules are val ...

  3. QML的默认属性default property

    qml中,普通的属性,需要添加属性名称,属性内容,如 color: “red” 默认属性则可以直接书写,去掉方括号,在写重用的QML组件式比较有用,例如将一个QmL外部资源封装好,内部具体的item, ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. iOS @property的默认属性

    我是一个比较懒的人,很多情况下@property都不喜欢加属性…所以必须了解默认情况下哪些是已经有的,哪些在需要时是必须要加的. 原文链接:http://blog.sina.com.cn/s/blog ...

  9. 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values

    对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...

随机推荐

  1. 华为“方舟编译器”到底是啥?一文看懂TA如何让手机性能再突破【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...

  2. luogu P2272 [ZJOI2007]最大半连通子图

    题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若 ...

  3. NRF51802蓝牙4.0BLE

    今天给大家介绍一款NRF51802的芯片 它是NRF51822的COSTDOWN精简版 本质上跟NRF51822是一致的,原厂为了给大客户节省成本而定制的一个版本 可以跟NRF51822软件硬件完全兼 ...

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

    最近一段时间上班忙的没日没夜的,不是披星戴月,就是头天早上出门,第二天早上回家...一直没出处时间来更新博客,码农之苦,说了都是泪,废话不多说,直接本主题,经过之前三篇的讲述,ios开发的东西大家或多 ...

  5. .net core 3.1简体中文语言包,英文只能提示变成中文!

    .net core 3.1简体中文语言包,英文只能提示变成中文!这个是我自己手动用谷歌翻译翻译的一批文档,已经把.net core3.1完全翻译了,由于是翻译器翻译,所以怕翻译有错漏,所以没有去掉英文 ...

  6. Docker私有仓库搭建与界面化管理

    一.关于Registry 官方的Docker hub是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去. 但是有时候我们的使用场景需要我们拥有一个私有的镜 ...

  7. docker-compose部署

    一.部署compose docker compose可以方便我们快捷高效地管理容器的启动.停止.重启等操作,它类似于linux下的shell脚本,基于yaml语法,在该文件里我们可以描述应用的架构,比 ...

  8. 11条MySQL规范,你知道的有几个?

    一.数据库命令规范 · 所有数据库对象名称必须使用小写字母并用下划线分割 · 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) · 数据库对象的命名 ...

  9. 基于 HTML5 WebGL 构建智能数字化城市 3D 全景

    前言 自 2011 年我国城镇化率首次突破 50% 以来,<新型城镇化发展规划>将智慧城市列为我国城市发展的三大目标之一,并提出到 2020 年,建成一批特色鲜明的智慧城市.截至现今,全国 ...

  10. ELK输出nginx的日志(未完成)

    我们先准备3台centos7服务器 171 做 elasticsearch,kibana的操作 172 做logstash 的操作 173 做nginx 的操作 软件 版本号 elasticsearc ...