In this lesson, you will learn how to implement business classes for your application using the Business Class Library. This library contains the most typical ready-to-use business classes. You will implement a custom Contact class by deriving from the Person class available in this library, and implement several additional properties. You will also learn the basics of automatic user interface construction based on data.
在本课中,您将学习如何使用 Business 类库为应用程序实现业务类。此库包含最典型的即用型业务类。您将通过从此库中可用的 Person 类派生来实现自定义联系人类,并实现多个附加属性。您还将学习基于数据的自动用户界面构造的基础知识。
Typically, business classes should be implemented in a platform-independent module project, so that the same objects will be available in both WinForms and ASP.NET applications. To simplify the implementation of XAF-specific classes, several Visual Studio templates are supplied. In this lesson, you will use the XPO Business Object template to implement a persistent business class. Right-click the Business Objects folder in the MySolution.Module project, and choose Add DevExpress Item | New Item... to invoke Template Gallery.Then select the XAF Business Object | XPO Business Object template, specify Contact.cs as the new item's name and press Add Item. As a result, you will get an automatically generated code file with a single class declaration.
通常,业务类应在独立于平台的模块项目中实现,以便 WinForms 和ASP.NET应用程序中都提供相同的对象。为了简化 XAF 特定类的实现,提供了多个可视化工作室模板。在本课中,您将使用 XPO 业务对象模板来实现持久性业务类。右键单击 MySolution.模块项目中的业务对象文件夹,然后选择"添加 DevExpress 项目" |新项目...调用模板库,然后选择 XAF 业务对象 |XPO 业务对象模板,将Contact.cs指定为新项目的名称,然后按"添加项"。因此,您将获得一个自动生成的代码文件,其中只有一个类声明。
The auto-generated Contact class is a BaseObject class descendant, which is one of the base persistent classes. You should inherit one of these classes when implementing a persistent class from scratch is required, or use Business Class Library classes (which are derived from the BaseObject class as well). For a general overview of the business class concept, refer to the Business Classes vs Database Tables topic.
- 自动生成的 Contact 类是 BaseObject 类后代,它是基持久性类之一。当需要从头开始实现持久类时,应继承这些类之一,或使用 Business 类库类(也派生自 BaseObject 类)。有关业务类概念的概述,请参阅业务类与数据库表主题。
Replace the automatically generated class declaration with the following code.
将自动生成的类声明替换为以下代码。
C#
VB.NET [DefaultClassOptions]
public class Contact : Person {
public Contact(Session session) : base(session) { }
private string webPageAddress;
public string WebPageAddress {
get { return webPageAddress; }
set { SetPropertyValue(nameof(WebPageAddress), ref webPageAddress, value); }
}
private string nickName;
public string NickName {
get { return nickName; }
set { SetPropertyValue(nameof(NickName), ref nickName, value); }
}
private string spouseName;
public string SpouseName {
get { return spouseName; }
set { SetPropertyValue(nameof(SpouseName), ref spouseName, value); }
}
private TitleOfCourtesy titleOfCourtesy;
public TitleOfCourtesy TitleOfCourtesy {
get { return titleOfCourtesy; }
set { SetPropertyValue(nameof(TitleOfCourtesy), ref titleOfCourtesy, value); }
}
private DateTime anniversary;
public DateTime Anniversary {
get { return anniversary; }
set { SetPropertyValue(nameof(Anniversary), ref anniversary, value); }
}
private string notes;
[Size()]
public string Notes {
get { return notes; }
set { SetPropertyValue(nameof(Notes), ref notes, value); }
}
}
public enum TitleOfCourtesy { Dr, Miss, Mr, Mrs, Ms };As you can see, the Contact class ancestor is changed from BaseObject to Person, and several custom properties are implemented. Note that the Contact constructor and the SetPropertyValue method is used in property setters. These are specifics of the eXpress Persistent Objects (XPO)
- 如您所见,联系人类祖先从"基本对象"更改为"人",并实现了多个自定义属性。请注意,在属性设置器中使用了 Contact 构造函数和 SetPropertyValue 方法。这些是电子压持久对象 (XPO) 的具体细节
object-relational mapper utilized by XAF. You can refer to the XPO Best Practices
XAF 使用的对象关系映射器。您可以参考 XPO 最佳实践
knowledge base article and the Simplified Property Syntax topic in the XPO documentation for details.
知识库文章和 XPO 文档中的简化属性语法主题的详细信息。
Note the use of the DefaultClassOptionsAttribute attribute. In this tutorial, this attribute means that the following capabilities will be available for the Contact business class.
请注意使用默认类选项属性属性。在本教程中,此属性表示以下功能将可用于联系人业务类。
- The Contact item will be added to the main form's navigation control. When clicking this item, a List View will be displayed. This List View represents a list of objects of the Contact type.
- The Contact item will be added to the submenu of the New () button when objects of another type are displayed in the List View. Click this item to invoke a Contact detail form and create a new Contact object.
- The Contact objects will be provided as a data source to generate reports (see Create a Report in Visual Studio).
- 联系人项将添加到主窗体的导航控件中。单击此项目时,将显示列表视图。此列表视图表示联系人类型的对象的列表。
- 当另一种类型的对象显示在列表视图中时,联系人项将添加到"新建(new_dropdown_btn)"按钮的子菜单中。单击此项目可调用"联系人详细信息"窗体并创建新的"联系人"对象。
- 联系人对象将作为数据源提供以生成报告(请参阅在 Visual Studio 中创建报表)。
To apply each of these options separately, use the NavigationItemAttribute, CreatableItemAttribute and VisibleInReportsAttribute attributes.
要单独应用每个选项,请使用导航项属性、可操作项属性和可见中报表属性属性。
Note
If you have CodeRush
注意
如果您有代码CodeRush
installed, you can use Code Templates when implementing business classes. Using Code Templates decreases code creation time, because it helps avoid having to type the entire code manually and allows you to create regular code sections with only a few keystrokes. To learn about the built-in Code Templates for eXpress Persistent Objects, refer to the XPO and XAF Templates topic.
Run the WinForms or ASP.NET application. You will see how the user interface is automatically generated using the specified data structures. There will be a navigation control allowing you to display the Contact list. You will be able to customize this collection using the corresponding editors. If you click the New button or double-click an existing record, the application will show a detail form (Detail View) filled with editors for each data field.
在实现业务类时,可以使用代码模板。使用代码模板可缩短代码创建时间,因为它有助于避免手动键入整个代码,并允许您只需几个击键即可创建常规代码部分。要了解 eXpress 持久对象的内置代码模板,请参阅 XPO 和 XAF 模板主题。
运行 WinForms 或ASP.NET应用程序。您将看到如何使用指定的数据结构自动生成用户界面。将有一个导航控件,允许您显示联系人列表。您将能够使用相应的编辑器自定义此集合。如果单击"新建"按钮或双击现有记录,应用程序将显示一个详细信息表单(详细信息视图),其中填充了每个数据字段的编辑器。
The following image demonstrates the Detail and List Views in the WinForms application.
下图演示了 WinForms 应用程序中的详细信息视图和列表视图。
Notice that many elements have been generated in an intuitive manner in very little time. The proper editors are created for data fields, and appropriate editors are used in the grid controls to display data. Note that a combo box editor has been created for Title Of Courtesy (an enumerator). Also note that captions have automatically been transformed from camel-case to space-separated strings, form titles are automatically updated, etc.
请注意,许多元素是在非常少的时间内以直观的方式生成的。为数据字段创建适当的编辑器,并在网格控件中使用适当的编辑器来显示数据。请注意,已为礼貌标题(枚举器)创建了组合框编辑器。另请注意,标题已自动从骆驼大小写转换为空格分隔的字符串,表单标题会自动更新,等等。
You can use the grid features to show, hide and rearrange columns, and apply grouping, filtering and sorting to a List View at runtime. In the WinForms application, you can customize the editor layout on the detail form as you like at runtime. For this purpose, right-click an empty space and select Customize Layout. You can now move editors to the required positions. To learn how to customize the editor layout at design time, refer to the Customize the View Items Layout topic. Additionally, you can refer to the View Items Layout Customization and List View Column Generation topics to see how the default Detail View layout and default List View column set are generated.
- 您可以使用网格要素来显示、隐藏和重新排列列,并在运行时对列表视图应用分组、筛选和排序。在 WinForms 应用程序中,您可以在运行时根据需要自定义详细信息窗体上的编辑器布局。为此,右键单击空白区域并选择"自定义布局"。您现在可以将编辑器移动到所需的位置。要了解如何在设计时自定义编辑器布局,请参阅自定义视图项布局主题。此外,还可以参考"查看项目布局自定义"和"列表视图列生成"主题,以查看如何生成默认"详细信息视图"布局和默认列表视图列集。
You can see the code demonstrated here in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
您可以在 MySolution.模块 |业务对象 |Contact.cs (Contact.vb) 文件的主演示安装与 XAF.默认情况下,主演示应用程序安装在 %PUBLIC%_文档_DevExpress 演示 19.2_组件_eXpressApp 框架_MainDemo 中。ASP.NET版本可在 http://demos.devexpress.com/XAF/MainDemo/ 在线获取%PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/
.
.