学习011-03-07 Create a Business Model in the XPO Data Model Designer(在 XPO 数据模型设计器中创建业务模型)

Create a Business Model in the XPO Data Model Designer(在 XPO 数据模型设计器中创建业务模型)

This topic provides step-by-step instructions on how to use the XPO Data Model Designer in XAF applications. We will create a simple business model consisting of two objects - Employee and Task. These objects will be linked with a one-to-many relationship. Then you will add XAF-specific attributes in code, and the application will be ready to use.
本主题提供有关如何在XAF应用程序中使用XPO数据模型设计器的分步说明。我们将创建一个由两个对象组成的简单业务模型——员工和任务。这些对象将以一对多的关系链接。然后,您将在代码中添加特定于XAF的属性,应用程序就可以使用了。

Note
This designer can also be used to generate a data model for a legacy database (see How to: Generate XPO Business Classes for Existing Data Tables).
此设计器还可用于为遗留数据库生成数据模型(请参阅如何:为现有数据表生成XPO业务类)。

If you prefer to watch a video rather than walk through these step-by-step instructions, you can find the corresponding tutorial on the DevExpress YouTube Channel: XAF: Start from Building a Data Model using the XPO Data Model Designer.
如果您更喜欢观看视频而不是浏览这些分步说明,您可以在DevExpressYouTube频道上找到相应的教程: XAF:从使用XPO数据模型设计器构建数据模型开始。

Create an XPO Data Model(创建XPO数据模型)

  • Create a new XAF solution using the DevExpress v24.1 XAF Template Gallery template.
    使用DevExpress v24.1 XAF模板库模板创建新的XAF解决方案。
  • Right-click the BusinessObjects folder located in the module project and choose Add | New Item. In the invoked Add New Item dialog, select the DevExpress ORM Data Model Wizard template located in the DevExpress category. Set the new item’s name to MySolutionDataModel.xpo and click Add. You will see that the MySolutionDataModel.xpo item is added, and the wizard dialog is invoked.
    右键单击位于模块项目中的BusinessObjects文件夹,然后选择Add|New Item。在调用的Add New Item对话框中,选择位于DevExpress类别中的DevExpress ORM数据模型向导模板。将新项目的名称设置为MySolutionDataModel. xpo,然后单击Add。您将看到添加了MySolutionDataModel.xpo项目,并调用了向导对话框。
  • In the invoked dialog, select Do not connect to a database and click Next, to skip the creation of a database connection using the XPO Data Model Wizard. The database connection is managed by XAF in your solution.
    在调用的对话框中,选择不连接到数据库,然后单击下一步,以跳过使用XPO数据模型向导创建数据库连接。数据库连接由解决方案中的XAF管理。
    在这里插入图片描述

As a result, an empty data model will be shown in the designer.
因此,设计器中将显示一个空数据模型。
在这里插入图片描述

Design the Employee Object(设计员工对象)

  • To add a new business class, drag the XpObject item from the toolbox to the designer’s surface.
    要添加新的业务类,请将XpObject项从工具箱拖到设计器的表面。
    在这里插入图片描述

  • Focus the newly added object. In the Properties window, set the object’s Name to Employee.
    聚焦新添加的对象。在属性窗口中,将对象的名称设置为员工。
    在这里插入图片描述

  • To add a new persistent property, drag the Field item from the Toolbox to the Employee object.
    要添加新的持久属性,请将Field项从Toolbox拖到Owe对象。
    在这里插入图片描述

  • Focus the newly added field. In the Properties window, set the field’s Name to FirstName.
    聚焦新添加的字段。在属性窗口中,将字段的名称设置为FirstName。
    在这里插入图片描述

Note that the Column Type is String by default.
请注意,列类型默认为字符串。

  • Repeat the two previous steps to add the LastName property.
    重复前面的两个步骤以添加LastName属性。
    在这里插入图片描述

  • Now add a field of a type other than string. Add the Birthday field, and set its Column Type to DateTime.
    现在添加字符串以外类型的字段。添加生日字段,并将其列类型设置为日期时间。
    在这里插入图片描述

  • Next, you will see how to add a calculated field. Drag the Persistent Alias item from the Toolbox to the Employee object.
    接下来,您将看到如何添加计算字段。将持久别名项目从工具箱拖到员工对象。
    在这里插入图片描述

  • Focus the newly added persistent alias. In the Properties window, set Name to FullName. Then, click the ellipsis button located to the right of the Expression setting. In the invoked Expression editor dialog, specify the [FirstName] + ’ ’ + [LastName] expression and click OK.
    聚焦新添加的持久别名。在属性窗口中,将名称设置为FullName。然后,单击表达式设置右侧的省略号按钮。在调用的表达式编辑器对话框中,指定[FirstName] + ’ ’ + [LastName]表达式并单击确定。
    在这里插入图片描述

Note
You can inherit the Employee class from the DevExpress.Persistent.BaseImpl.Person class, instead of creating it from scratch. The XPO Data Model Designer supports the use of persistent classes from external assemblies as base classes and property types. To add business classes from the Business Class Library (or your own class library), do the following.
您可以从DevExpress. Perst.BaseImpl.Person类继承员工类,而不是从头开始创建它。XPO数据模型设计器支持使用来自外部程序集的持久类作为基类和属性类型。要从业务类库(或您自己的类库)添加业务类,请执行以下操作。

  • Right-click the free space on the design surface. In the invoked context menu, choose Add Assembly.
    右键单击设计图面上的可用空间。在调用的上下文菜单中,选择添加程序集。
  • Select the assembly that contains business classes and click Open. You can choose the DevExpress.Persistent.BaseImpl.Xpo.v24.1.dll assembly that ships with XAF, located in the %PROGRAMFILES%\DevExpress 24.1\Components\Bin\Framework path, or your custom assembly.
    选择包含业务类的程序集,然后单击打开。您可以选择XAF随附的DevExpress.持久性. BaseImpl.Xpo.v24.1.dll程序集(位于%PROGRAMFILES%\DevExpress 24.1\Components\bin\Framework路径)或您的自定义程序集。
  • In the Select Types dialog, choose the persistent classes to import from the assembly and click OK.
    在选择类型对话框中,选择要从程序集导入的持久类,然后单击确定。
    These actions will import classes in read-only mode. Only persistent fields are displayed for these classes, and reference property types correspond to the underlying database types. However, you will be able to use the added classes as ancestors, and inherit new persistent classes from them in the Designer. To specify inheritance, select the Inheritance item in the Toolbox and draw a line from a descendant to a base class. Additionally, you will be able to use these classes as property types.
    这些操作将以只读模式导入类。这些类只显示持久字段,引用属性类型对应于基础数据库类型。但是,您将能够使用添加的类作为祖先,并在设计器中从它们继承新的持久类。要指定继承,请选择工具箱中的继承项,并从后代到基类画一条线。此外,您将能够将这些类用作属性类型。

Design the Task Object(设计任务对象)

  • To add another business class, drag the XpObject item from the toolbox to the designer’s surface. Focus the newly added object. In the Properties window, set the object’s Name to Task.
    要添加另一个业务类,请将XpObject项从工具箱拖到设计器的表面。聚焦新添加的对象。在属性窗口中,将对象的名称设置为任务。
    在这里插入图片描述

  • Add the Subject and Description persistent properties of the string type using the approach described in the Design the Employee Object section.
    使用设计员工对象部分中描述的方法添加字符串类型的主题和描述持久属性。
    在这里插入图片描述

  • Next, see how to apply attributes to persistent properties. Focus the Description field. In the Properties window, expand the DBType category, and set Size to Unlimited.
    接下来,了解如何将属性应用于持久属性。关注描述字段。在属性窗口中,展开DBType类别,并将大小设置为无限。
    在这里插入图片描述

As a result, the SizeAttribute will be applied to the Description property in the underlying code. The attribute’s parameter will be set to SizeAttribute.Unlimited.
因此,SizeAtcade将应用于底层代码中的Description属性。该属性的参数将设置为SizeAttory. Unlimited。

Add a One-to-Many Association(添加一对多关联)

  • Add the AssignedTo persistent property of the Employee type to the Task class. This property will represent the “one” part of the one-to-many association.
    将员工类型的AssignedTo持久属性添加到任务类。此属性将表示一对多关联的“一”部分。
    在这里插入图片描述

  • In the Toolbox, focus the Association Object item. Draw a line from Employee to Task, to create the association.
    在工具箱中,聚焦关联对象项。从员工到任务画一条线,以创建关联。
    在这里插入图片描述

Note
Alternatively, you can use the XpObject’s title bar context menu to create an association. For details, refer to the Association section of the Data Model Designer topic.
或者,您可以使用XpObject的标题栏上下文菜单来创建关联。有关详细信息,请参阅数据模型设计器主题的关联部分。

  • Save the changes and close the designer.
    保存更改并关闭设计器。

Add XAF-Specific Attributes in Code(在代码中添加XAF特定的属性)

  • In the Solution Explorer, expand the BusinessObjects\MySolutionDataModelCode folder. This folder contains the code generated by the designer. Open the Employee.cs (Employee.vb) file. Decorate the Employee class with the DefaultClassOptionsAttribute and ImageNameAttribute attributes to the Employee object. As a result, the Employee object will be added to the Navigation System, and an icon from the built-in library will be associated with this object.
    在解决方案资源管理器中,展开BusinessObjects\MySolutionDataModelCode文件夹。该文件夹包含设计器生成的代码。打开员工. cs(员工.vb)文件。使用员工对象的DefaultClassOptionsAttribute和ImageNameAtcade属性装饰员工类。结果,员工对象将被添加到导航系统中,并且内置库中的一个图标将与该对象相关联。
C#
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions, ImageName("BO_Employee")]
public partial class Employee {
    public Employee(Session session) : base(session) { }
    public Employee() : base(Session.DefaultSession) { }
    public override void AfterConstruction() { base.AfterConstruction(); }
}
  • Open the Task.cs file. Decorate the Task class with the DefaultClassOptions and ImageName attributes.
    打开 Task. cs文件。使用DefaultClassOptions和ImageName属性装饰任务类。
C#
using DevExpress.Persistent.Base;
// ...
[DefaultClassOptions, ImageName("BO_Task")]
public partial class Task {
    public Task(Session session) : base(session) { }
    public Task() : base(Session.DefaultSession) { }
    public override void AfterConstruction() { base.AfterConstruction(); }
}

Note
You can add more custom code to the auto-generated classes (e.g., add Action methods or override base class methods). Do not change the code located in files with the designer suffix - they contain designer-generated code, and should not be modified manually. The classes generated are declared as partial. Designed and custom class parts are located in different files.
您可以向自动生成的类添加更多自定义代码(例如,添加Action方法或覆盖基类方法)。不要更改位于带有设计器后缀的文件中的代码——它们包含设计器生成的代码,不应手动修改。生成的类被声明为部分类。设计和自定义类部分位于不同的文件中。

Important
You cannot apply attributes to properties in the partial class’ code. Instead, use the designer (see the next section).
您不能将属性应用于部分类代码中的属性。相反,请使用设计器(请参阅下一节)。

Add XAF-Specific Attributes in the Designer(在设计器中添加XAF特定属性)

Alternatively, you can use the designer to apply attributes. Focus the required class or field and specify the Custom Attributes setting in the Properties window.
或者,您可以使用设计器应用属性。聚焦所需的类或字段并在属性窗口中指定自定义属性设置。
在这里插入图片描述

Run the Application(运行应用程序)

Now you can run the Windows Forms and ASP.NET Web Forms applications to see the result. These applications are completely based on the business model specified in the XPO Data Model Designer.
现在您可以运行Windows窗体和ASP.NET Web窗体应用程序来查看结果。这些应用程序完全基于XPO数据模型设计器中指定的业务模型。

  • Windows Forms
    在这里插入图片描述

  • ASP.NET Web Forms
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

汤姆•猫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值