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 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=T209960
完整的示例项目可在 DevExpress 代码示例数据库中找到,http://www.devexpress.com/example=T209960
.
Tip 提示
A similar example for XPO is available in the How to: Initialize Business Objects with Default Property Values in XPO topic.
XPO 的类似示例在 XPO 主题中的"如何:使用默认属性值初始化业务对象"主题中提供。
Simple Property
简单属性
You can support an IXafEntityObject interface in your business classes. This interface declares the IXafEntityObject.OnCreated method intended for object initialization. The OnCreated method is called only once for an object - after the object is created. Whenever you need to initialize an object, place the initialization code into the OnCreated method body. The following code snippet demonstrates how simple value properties can be initialized.
您可以在业务类中支持 IXafEntityObject 接口。此接口声明用于对象初始化的 IXafEntityObject.OnCreated 方法。在创建对象后,仅对对象调用一次 OnCreated 方法。每当需要初始化对象时,将初始化代码放入 OnCreated 方法正文中。以下代码段演示如何初始化简单值属性。
public class Contact : Person, IXafEntityObject {
//...
void IXafEntityObject.OnCreated() {
FirstName = "Sam";
TitleOfCourtesy = TitleOfCourtesy.Mr;
}
void IXafEntityObject.OnLoaded() { }
void IXafEntityObject.OnSaving() { }
}
To see another example of initializing a simple property, refer to the Initialize a Property After Creating an Object (EF) tutorial lesson.
要查看初始化简单属性的另一个示例,请参阅创建对象 (EF) 教程课后初始化属性。
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 IObjectSpace.FindObject method of the object's Object Space. To access the Object Space from the business object code, you should support the IObjectSpaceLink interface. The following code snippet demonstrates how to initialize reference properties with new and existing objects.
引用属性的初始化不同于简单属性的初始化,主要是因为您可能需要获取对现有对象的引用。为此,请使用对象对象空间的 IObjectSpace.FindObject 方法。要从业务对象代码访问对象空间,应支持 IObjectSpaceLink 接口。以下代码段演示如何使用新对象和现有对象初始化引用属性。
public class Contact : Person, IXafEntityObject, IObjectSpaceLink {
//...
void IXafEntityObject.OnCreated() {
// ...
Address1 = objectSpace.CreateObject<Address>();
Address1.Country = objectSpace.FindObject<Country>(CriteriaOperator.Parse("Name = 'USA'"));
if (Address1.Country == null) {
Address1.Country = objectSpace.CreateObject<Country>();
Address1.Country.Name = "USA";
}
Manager = objectSpace.FindObject<Contact>(
CriteriaOperator.Parse("FirstName = 'John' && LastName = 'Doe'"));
}
void IXafEntityObject.OnLoaded() { }
void IXafEntityObject.OnSaving() { }
private IObjectSpace objectSpace;
IObjectSpace IObjectSpaceLink.ObjectSpace {
get { return objectSpace; }
set { objectSpace = value; }
}
}
Collection Property
集合属性
The following code snippet demonstrates how to populate the Phones collection with predefined phone numbers.
以下代码段演示如何使用预定义的电话号码填充电话集合。
public class Contact : Person, IXafEntityObject, IObjectSpaceLink {
//...
void IXafEntityObject.OnCreated() {
// ...
PhoneNumber phone1 = objectSpace.FindObject<PhoneNumber>(
CriteriaOperator.Parse("Number = '555-0101'"));
PhoneNumber phone2 = objectSpace.FindObject<PhoneNumber>(
CriteriaOperator.Parse("Number = '555-0102'"));
PhoneNumbers.Add(phone1);
PhoneNumbers.Add(phone2);
}
void IXafEntityObject.OnLoaded() { }
void IXafEntityObject.OnSaving() { }
private IObjectSpace objectSpace;
IObjectSpace IObjectSpaceLink.ObjectSpace {
get { return objectSpace; }
set { objectSpace = value; }
}
}
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 a 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 {
// ...
public MasterObject MasterObject {
get { return masterObject; }
set {
if (master == value) return;
masterObject = value;
if (value != null) {
this.SomeProperty = value.DefaultForChildren;
}
}
}
}
Since the reference property of a child object will not be initialized until committing changes, it is necessary to use a ViewController to initialize the child object depending on the master object (see How to: Initialize an Object Created Using the New Action).
由于子对象的引用属性在提交更改之前不会初始化,因此有必要使用 ViewController 根据主对象初始化子对象(请参阅:使用"新操作"创建的对象初始化)。
How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象的更多相关文章
- 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 ...
- 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values
对于任何语言来说,函数都是一个重要的组成部分.在ES6以前,从JavaScript被创建以来,函数一直没有大的改动,留下了一堆的问题和很微妙的行为,导致在JavaScript中使用函数时很容易出现错误 ...
- QML的默认属性default property
qml中,普通的属性,需要添加属性名称,属性内容,如 color: “red” 默认属性则可以直接书写,去掉方括号,在写重用的QML组件式比较有用,例如将一个QmL外部资源封装好,内部具体的item, ...
- Implement Property Value Validation in Code 在代码中实现属性值验证(XPO)
This lesson explains how to set rules for business classes and their properties. These rules are val ...
- 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 ...
- Entity Framework Code-First(10.3):Property Mappings
Property Mappings using Fluent API: Here, we will learn how to configure properties of an entity cla ...
随机推荐
- Yii2 负载均衡找不到JS,CSS
在部署项目的时候,用了2台服务器.请求的时候用了负载均衡,导致 YII2 的静态文件(js,css...)报 404 ,原因是: 请求一个页面时 A服务器 去处理,但是静态资源缺请求到了 B服务器 , ...
- adb adb monkey命令及介绍
1.adb的组成部分 守护进程,客户端,服务器端` 2.Monkey程序是Google公司提供的一个压力和稳定性测试的工具 3.命令 命令 参数 功能 adb version 查看当前a ...
- 西瓜哥:公有云也“All-Flash”?
本文转载自 高端存储知识 Gartner在2018年Market Insight: Preparing for the SSD Rise and HDD Demise一文中指出:当闪存介质降到HDD每 ...
- 一道随机函数题:由rand5()生成rand7()
题目:已知rand5()函数能随机等概率的生成0, 1, 2, 3, 4,利用rand5()函数编写一个rand7()函数实现相似的功能. 分析:其实就是利用rand5()组合成一个更大范围的数,之后 ...
- [TimLinux] Python 类型与运算
1. 内建(built-in)数据类型种类 数字类型:int(), float() 顺序(sequence): 字符串:str() 元祖:tuple() 列表:list() 字典:dict() 集合: ...
- 捅娄子了,写个bug被国家信息安全漏洞共享平台抓到了?
摸不了鱼了 2019 年 11 月 26 日,本来应该是无比平静的一天,开开会,改改bug,摸摸鱼之后等着下班.刷着新闻的间隙,手机的消息提示音响了起来,收到了一条邮件,平时收到邮件我都会选择稍后处理 ...
- Day 06 流程控制和爬虫基础2
目录 if 判断 单分支结构 双分支结构 多分支结构 for循环 for循环的基本用法 for循环嵌套 break continue 爬虫基础2 爬取豆瓣TOP250 爬取豆瓣数据接口(异步数据) 爬 ...
- ruby on rails测试
Rspec测试 Rspec(基本测试) 安装 group :development, :test do gem 'rspec-rails', '~> 3.5' end rails generat ...
- c++之基础知识
一.变量 作用:给一段指定的内存空间,方便操作这段内存. 语法:数据类型 变量名 = 初始值.int a = 10; 二.常量 作用:用于记录程序中不可更改的数据 c++定义常量有两种方式: #def ...
- PythonI/O进阶学习笔记_8.python的可迭代对象和迭代器、迭代设计模式
content: 1.什么是迭代协议 2. 什么是迭代器(Iterator)和可迭代对象(Iterable) 3. 使用迭代器和可迭代对象 4. 创建迭代器和可迭代对象 5. 迭代器设计模式 一 ...