In this lesson, you will learn how to set relationships between business objects. For this purpose, the Task business class will be implemented and a Many-to-Many relationship will be set between the Contact and Task objects. You will also learn the basics of automatic user interface construction for the referenced objects.
在本课中,您将学习如何设置业务对象之间的关系。为此,将实现 Task 业务类,并在"联系人"和"任务"对象之间设置多对多关系。您还将学习引用对象的自动用户界面构造基础知识。
- To add the Task business class to the application, you can use the Task class from the Business Class Library. Since you need to set a relationship between the Contact and Task objects, you need to customize the Task class implementation. Inherit from this class and add the Contacts collection property, as shown in the following code.
- 要将 Task 业务类添加到应用程序,可以使用 Business 类库中的"任务"类。由于您需要设置"联系人"和"任务"对象之间的关系,因此需要自定义 Task 类实现。从此类继承并添加"联系人"集合属性,如以下代码所示。
using DevExpress.ExpressApp.Model; // ... [DefaultClassOptions] [ModelDefault("Caption", "Task")] public class DemoTask : Task { public DemoTask(Session session): base(session) { } [Association("Contact-DemoTask")] public XPCollection<Contact> Contacts { get { return GetCollection<Contact>(nameof(Contacts)); } } }
Important
Do not modify the XPCollection property declaration demonstrated above. Manipulating the collection or introducing any additional settings within the declaration may cause unpredictable behavior.
重要
不要修改上面演示的 XPCollection 属性声明。操作集合或在声明中引入任何其他设置可能会导致不可预知的行为。In this code, the AssociationAttribute is applied to the XPCollection type Contacts property, representing the collection of associated Contacts. The Association attribute is required when setting a relationship between objects. Note that the Contacts property getter implementation - the GetCollection method - is used to return a collection.
在此代码中,关联属性应用于 XPCollection 类型"联系人"属性,表示关联联系人的集合。设置对象之间的关系时,需要关联属性。请注意,联系人属性 getter 实现 - GetCollection 方法 - 用于返回集合。
Note
The ModelDefaultAttribute attribute, which is applied to the DemoTask class, specifies the "Task" value for the Caption property of the Application Model‘s BOModel | DemoTask node. Generally, you can specify any property of the Application Model‘s BOModel | <Class> or BOModel | <Class> | OwnMembers | <Member> node, by applying the ModelDefault attribute to a business class or its member.
注意
应用于 DemoTask 类的模型默认属性属性为应用程序模型的 BOModel 的 Caption 属性指定"Task"值 |演示任务节点。通常,您可以指定应用程序模型的 BOModel 的任何属性 |<Class>或 BOModel |<Class>*自己的会员 |<Member>节点,通过将 ModelDefault 属性应用于业务类或其成员。
- Modify the Contact class implementation: add the Tasks property as the second part of the Contact-DemoTask relationship. Note that the Association attribute must be applied to this property as well. The following code demonstrates a code snippet from the Contact class implementation.
- 修改联系人类实现:将 Tasks 属性添加为联系人-演示任务关系的第二部分。请注意,关联属性也必须应用于此属性。以下代码演示了联系人类实现的代码段。
[DefaultClassOptions] public class Contact : Person { //... [Association("Contact-DemoTask")] public XPCollection<DemoTask> Tasks { get { return GetCollection<DemoTask>(nameof(Tasks)); } } }
The code above will automatically generate the required intermediate tables and relationships.
上述代码将自动生成所需的中间表和关系。
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. Invoke the Contact Detail View or Task Detail View. Add tasks to a Contact object‘s Tasks collection, or contacts to a Task object‘s Contacts collection. To apply the assignment, use the Link button that accompanies these collections.
- 在实现业务类时,可以使用代码模板。使用代码模板可缩短代码创建时间,因为它有助于避免手动键入整个代码,并允许您只需几个击键即可创建常规代码部分。要了解 eXpress 持久对象的内置代码模板,请参阅 XPO 和 XAF 模板主题。
- 运行 WinForms 或ASP.NET应用程序。调用联系人详细信息视图或任务详细信息视图。将任务添加到"联系人"对象的"任务"集合,或将联系人添加到"任务"对象的"联系人"集合中。要应用分配,请使用这些集合附带的链接按钮。
You can see the code demonstrated in this lesson in the Contact.cs (Contact.vb) and DemoTask.cs (DemoTask.vb) files 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/
您可以在本课中演示的代码,请参阅与 XAF 一起安装的主演示的Contact.cs (Contact.vb) 和DemoTask.cs (DemoTask.vb) 文件中。默认情况下,主演示应用程序安装在 %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/ 中。ASP.NET版本可在 http://demos.devexpress.com/XAF/MainDemo/ 在线获取
.
.
原文地址:https://www.cnblogs.com/foreachlife/p/Many-to-ManyRelationship.html