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. js中的Object.assign接受两个函数为参数的时候会发生什么?

    缘由 今天看到一段代码 return Object.assign(func1, func2); 心生疑惑,为什么 Object.assign 的参数可以是函数? 于是有了下面这一堆东西,其实都是老生常 ...

  2. 使用Docker测试静态网站

    参考书籍 :第一本docker书[澳]James Turnbull  1.Sample网站的初始Dockerfile 文件目录如下: Dockerfile文件代码: 安装nginx 在容器中创建一个目 ...

  3. 你真的了解JSON吗?

    一.JSON——JavaScript Object Notation JSON 是一种语法用来序列化对象.数组.数值.字符串.布尔值和null .它基于 JavaScript 语法,但与之不同:一些J ...

  4. k8s 开船记-触礁:四涡轮发动机撞坏3个引发502故障

    (图片来自网络) 非常抱歉,这次开船触礁故障给您带来麻烦了,请您谅解. 在我们昨天发布 k8s 开船记首航博文后,有园友在评论中发来贺词——“泰坦尼克号出发了[狗头]”,借此吉言,今天船就触礁了,还好 ...

  5. 【Vuejs】351- 带你解析vue2.0的diff算法

    前言 vue2.0加入了virtual dom,有向react靠拢的意思.vue的diff位于patch.js文件中,该算法来源于snabbdom,复杂度为O(n).了解diff过程可以让我们更高效的 ...

  6. 【CuteJavaScript】ES2019 新特性汇总

    最近 ECMAScript2019,最新提案完成:tc39 Finished Proposals,我这里也是按照官方介绍的顺序进行整理,如有疑问,可以查看官方介绍啦~ 另外之前也整理了 <ES6 ...

  7. 【Web技术】276- WebView缓存原理分析和应用

    前言 混合式开发,在产品体验以及页面加载速度的体验上已经非比以往的.今日早读文章由@unclechen分享. 正文从这开始- 一.背景 现在的App开发,或多或少都会用到Hybrid模式,到了WebV ...

  8. 【JPA】字段访问、属性访问及混合访问

    [JPA]字段访问.属性访问及混合访问 转载:https://www.cnblogs.com/yangchongxing/p/10120318.html 1.字段访问 注解字段,通过反射来获得和设置字 ...

  9. 【JPA】开始

    Java SE中使用 实体Bean package cn.ycx.entity; import javax.persistence.Entity; import javax.persistence.I ...

  10. [Python]实现字符串倒序的三种方法

    a=" 1: print(a[::-1]) 2: b=list(a) b.reverse() print(''.join(b)) 3: c=len(a)-1 str_1=[] while(c ...