Implement Custom Business Classes and Reference Properties (XPO)
- 4 minutes to read
Tip
For .NET 6 applications, see: Implement Custom Business Classes and Reference Properties (XPO, .NET 6).
In this lesson, you will learn how to implement business classes from scratch. For this purpose, the Department and Position business classes will be implemented. These classes 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 lesson.
Add the following Department and Position persistent classes to the Contact.cs (Contact.vb) file.
[DefaultClassOptions] [System.ComponentModel.DefaultProperty(nameof(Title))] public class Department : BaseObject { public Department(Session session) : base(session) { } private string title; public string Title { get { return title; } set { SetPropertyValue(nameof(Title), ref title, value); } } private string office; public string Office { get { return office; } set { SetPropertyValue(nameof(Office), ref office, value); } } } [DefaultClassOptions] [System.ComponentModel.DefaultProperty(nameof(Title))] public class Position : BaseObject { public Position(Session session) : base(session) { } private string title; public string Title { get { return title; } set { SetPropertyValue(nameof(Title), ref title, value); } } }
These new classes are persistent, as they are BaseObject class descendants.
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.
- 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.
Add the Department and Position properties to the Contact class. The following code demonstrates this.
[DefaultClassOptions] public class Contact : Person { //... private Department department; public Department Department { get {return department;} set {SetPropertyValue(nameof(Department), ref department, value);} } private Position position; public Position Position { get {return position;} set {SetPropertyValue(nameof(Position), ref position, value);} } //... }
The Contact class now exposes the Position and Department reference properties.
Note
CodeRush includes a number of Code Templates that help generate business classes or their parts with a few keystrokes. To learn about the Code Templates for eXpress Persistent Objects, refer to the following help topic: XPO and XAF Templates.
Run the WinForms or ASP.NET Web Forms application. You will see how the user interface is automatically generated using the specified data structures. The navigation control will contain new Department and Position items, which will allow you to access Department and Position objects. Note that in the Contact Detail View, a lookup editor has been created for Department and 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 required Department (Position) for the current Contact, and add new Department (Position) objects using the New button. In addition, you will also be able to edit existing Department (Position) objects by holding down SHIFT+CTRL and clicking the selected object.
You can see the code demonstrated in this lesson 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 22.1\Components\XAF\MainDemo by default. The ASP.NET Web Forms version is available online at https://demos.devexpress.com/XAF/MainDemo.
Next Lesson: Add a Class from the Business Class Library