Implement Custom Business Classes and Reference Properties实现自定义业务类和引用属性(EF)
In this lesson, you will learn how to implement business classes from scratch. For this purpose, the Position business class will be implemented. This class will be used in the Contact class, implemented previously. You will also learn the basics of automatic user interface construction for referenced objects.
在本课中,您将学习如何从头开始实现业务类。为此,将实现职位业务类。此类将用于以前实现的"联系人"类。您还将学习引用对象的自动用户界面构造基础知识。
Note
Before proceeding, take a moment to review the Inherit from the Business Class Library Class (EF) lesson.
在本课中,您将学习如何从头开始实现业务类。为此,将实现职位业务类。此类将用于以前实现的"联系人"类。您还将学习引用对象的自动用户界面构造基础知识。
-
Add the Position class as shown in the Inherit from the Business Class Library Class (EF) lesson. Replace the auto-generated code with the following.
-
添加职位类,如从业务类库类 (EF) 的继承课程所示。将自动生成的代码替换为以下内容。
using System; using System.Collections.Generic; using System.ComponentModel; using DevExpress.Persistent.Base; namespace MySolution.Module.BusinessObjects { [DefaultClassOptions] [DefaultProperty(nameof(Title))] public class Position { public Position() { Contacts = new List<Contact>(); } [Browsable(false)] public Int32 ID { get; protected set; } public String Title { get; set; } public virtual IList<Contact> Contacts { get; set; } } }
Note that the Contact collection property is declared as virtual.
-
请注意,联系人集合属性声明为虚拟。
-
Note
The DefaultProperty attribute is used in the code above. This attribute is used to specify the default property of the class. You can specify the most descriptive property of your class in the DefaultProperty attribute, and its values will be displayed in the following.
注意
DefaultProperty 属性用于上述代码。此属性用于指定类的默认属性。您可以在 DefaultProperty 属性中指定类最具描述性的属性,其值将显示在以下内容中。- Detail form captions
- The leftmost column of a List View
- The Lookup List Views (These Views will be explained in the last step of this lesson.)
- 详细信息表单标题
- 列表视图的最左侧列
- 查找列表视图(这些视图将在本课的最后一步中介绍。
Refer to the Data Annotations in Data Model topic for additional information.
有关详细信息,请参阅数据模型中的数据注释主题。
Important
You can use the XAF Business Object | EF Business Object project item template from the Template Gallery to add an entity that supports IXafEntityObject, IObjectSpaceLink and INotifyPropertyChanged interfaces, which may be useful in many scenarios.
-
Register the new class in DbContext. Edit the BusinessObjects\MySolutionDbContext.cs file as shown below.
重要
您可以使用 XAF 业务对象 |从模板库中添加 EF 业务对象项目项模板,以添加支持 IXafEntityObject、IObjectSpaceLink 和 INotifyPropertyChanged 接口的实体,这在许多情况下可能很有用。
- 在 DbContext 中注册新类。编辑业务对象_MySolutionDbContext.cs 文件,如下所示。
-
public class MySolutionDbContext : DbContext { //... public DbSet<Position> Positions { get; set; } }
-
Add the Position property to the Contact class. The following code demonstrates this.
-
将"位置"属性添加到"联系人"类。以下代码演示了这一点。
public class Contact : Person { //... public virtual Position Position { get; set; } }
The Contact class now exposes the Position reference property. Note that this property is declared as virtual.
联系人类现在公开"位置"引用属性。请注意,此属性声明为虚拟。
-
Run the WinForms or ASP.NET application. You will see how the user interface is automatically generated using the specified data structures. The navigation control will contain a new Position item, which will allow you to access Position objects. Note that in the Contact Detail View, a lookup editor has been created for Position. In this editor, a special type of View, Lookup List View, is used. Typically, this View has a single column corresponding to the class' default property. Using the lookup editor, you can select the Position for the current Contact, and add new Position objects using the New button. In addition, you will also be able to edit the existing Position object by holding down SHIFT+CTRL and clicking the selected object.
-
运行 WinForms 或ASP.NET应用程序。您将看到如何使用指定的数据结构自动生成用户界面。导航控件将包含一个新的"位置"项,该项将允许您访问"位置"对象。请注意,在"联系人详细信息"视图中,已为"位置"创建了查找编辑器。在此编辑器中,使用特殊类型的视图"查找列表视图"。通常,此视图具有对应于类的默认属性的单个列。使用查找编辑器,您可以选择当前联系人的位置,并使用"新建"按钮添加新"位置"对象。此外,您还可以通过按住 SHIFT_CTRL 并单击所选对象来编辑现有位置对象。
You can see the code demonstrated in this lesson in the EFDemo.Module | Data | Contact.cs (Contact.vb) and Position.cs (Position.vb) files of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\EFDemoCodeFirst.
您可以在 EFDemo.模块 |数据 |Contact.cs (Contact.vb) 和 Position.cs (位置.vb) 文件与 XAF 一起安装的 EF 演示(代码优先)。默认情况下,EF 演示(代码优先)应用程序安装在
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码:

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端