学习011-03-12 Initialize Business Objects with Default Property Values in XPO(在 XPO 中使用默认属性值初始化业务对象)

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
Similar example for EF Core is available in the following help topic: How to: Initialize Business Objects with Default Property Values in Entity Framework Core
以下帮助主题中提供了EF Core的类似示例:如何:在Entity Framework Core中使用默认属性值初始化业务对象

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.
所有基本持久类都派生自Persis entBase类。该类公开了用于对象初始化的Persis entBase. AfterBuild方法。AfterBuild方法只对对象调用一次——在对象创建之后。每当您需要初始化对象时,您应该覆盖这些方法并将初始化代码放入其主体中。由于此方法专为初始化而设计,因此在为对象属性赋值时无需检查当前对象状态。以下代码片段演示了如何初始化简单的值属性。

C#
public class Contact : Person {
//...
    public override void AfterConstruction() {
        base.AfterConstruction();


        FirstName = "Sam";
        TitleOfCourtesy = TitleOfCourtesy.Mr;
    }
}

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.
引用属性的初始化不同于简单属性的初始化,主要是因为您可能需要获取对现有对象的引用。为此,请使用对象的Session. FindObject方法。

The following code snippet demonstrates how to initialize Employee’s Address1 and Manager reference properties with new and existing objects:
以下代码片段演示了如何使用新的和现有的对象初始化 Employee的 Address1和管理器引用属性:

File: MySolution.Module\BusinessObjects\Contact.cs

C#
using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;
// ...
public class Employee : Person {
    //...
    public override void AfterConstruction() {
        base.AfterConstruction();
        Address1 = new Address(Session);
        Address1.Country = Session.FindObject<Country>(CriteriaOperator.Parse("Name = &#39;USA&#39;"));
        if(Address1.Country == null) {
            Address1.Country = new Country(Session);
            Address1.Country.Name = "USA";
        }
        Manager = Session.FindObject<Employee>(CriteriaOperator.Parse(
            "FirstName = &#39;John&#39; && LastName = &#39;Doe&#39;"));
    }
}

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方法。以下代码片段演示了如何使用预定义的电话号码填充电话集合。

C#
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(初始化通过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.
在某些情况下,您可能只需要初始化专门通过New Action创建的对象。要了解如何执行此操作,请参阅如何:初始化使用New Action创建的对象帮助主题。

*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中设置子对象属性的默认值。

C#
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.
不可能在子对象的AfterBuild方法中获取父对象值,因为该方法是在从外部代码初始化任何属性之前调用的。如果您需要根据分配的父对象执行一些代码,请在ChildObject. MasterObject属性设置器或XPBaseCollection.ColliceChanged事件处理程序中执行此操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值