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. 技术谈 | SDN 和 NFV 之间的爱与恨

    部分开发者经常混淆 SDN 和 NFV,无法看清他们的关系.今天,小编搬出华为技术专家的一篇大稿,给大家掰扯掰扯:SDN 和 NFV 究竟是什么关系. ----文/闫长江 什么是 SDN 回到基本的概 ...

  2. PyTorch最佳实践,怎样才能写出一手风格优美的代码

    [摘要] PyTorch是最优秀的深度学习框架之一,它简单优雅,非常适合入门.本文将介绍PyTorch的最佳实践和代码风格都是怎样的. 虽然这是一个非官方的 PyTorch 指南,但本文总结了一年多使 ...

  3. 使用react-breadcrumbs-dynamic

    这是完全独立于路由器的解决方案,你可以将其与任何版本的React Router(2或3或4)或任何其他用于React的路由库一起使用,或者完全不进行路由.您只需要指定面包屑项目及其道具的组件.然而道具 ...

  4. SpringBoot整合log4j2进行日志配置及防坑指南

    写在前面 最近项目经理要求将原先项目中的日志配置logBack,修改为log4j2,据说是log4j2性能更优于logback,具体快多少,网上有说快10多倍,看来还是很快的,于是新的一波挑战又开始了 ...

  5. Charles Fiddler使用

    http://blog.devtang.com/2015/11/14/charles-introduction/ Charles 从入门到精通 http://www.infoq.com/cn/arti ...

  6. 教你们学习一个最简单又企业最需要的服务-crond

    第13章 定时任务的介绍 13.1 定时任务的分类 13.1.1 系统实现定时任务的配置 [root@oldboyedu ~] # cd /etc/cron. cron.d/ cron.daily/ ...

  7. mui 顶部选项卡的两种切换方式

    mui 顶部选项卡的两种切换方式 第一种main页面 <!DOCTYPE html> <html> <head> <meta charset="ut ...

  8. Java多态之向上转型

    目录 Java多态之向上转型 多态的优点 向上转型 概念 向上转型好在哪 Java多态之向上转型 多态性是面向对象的第三大特征. 多态的优点 改善代码的组织结构和可读性. 能够创建可扩展的程序.(随时 ...

  9. 【Web技术】281- 滴滴开源小程序框架 Mpx2.0

    滴滴Mpx框架负责人@hiyuki,滴滴出行网约车webapp乘客团队的负责人,也是滴滴开源的小程序框架Mpx的负责人和核心作者 Mpx是一款致力于提高小程序开发体验和效率的增强型小程序框架,目前在滴 ...

  10. robotframework配置邮箱服务器

    1.登录邮箱以腾讯企业邮箱为例:开启smtp服务并获得邮箱的客户端授权码 用户名:18890260218@163.com 客户端授权码:admin123 2.进入系统管理-->GO to plu ...