Inherit from the Business Class Library Class (EF Core)
- 5 minutes to read
This lesson explains how to use the Business Class Library to implement business classes for your application. The topic also describes the basics of automatic user interface construction based on data.
Before you proceed, familiarize yourself with the following concepts:
- List View
- List View displays the collection of objects of the same class in the user interface of your application.
- Detail View
- Detail View displays a particular business class object.
For additional information on these concepts refer to the following topic: Views.
Step-by-Step Instructions
Expand the MySolution.Module project and right-click the Business Objects folder. Choose Add | Class…. Specify Contact.cs as the new class name and click Add.
Business classes do not depend on the application UI. Implement them in a platform-independent module project. This way other XAF or non-XAF applications can share business objects.
Replace the generated class declaration with the code sample below:
using DevExpress.ExpressApp.DC; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl.EF; namespace MySolution.Module.BusinessObjects { [DefaultClassOptions] public class Contact : Person { public Contact() { } string webPageAddress; public string WebPageAddress { get => webPageAddress; set => SetPropertyValue(ref webPageAddress, value); } string nickName; public string NickName { get => nickName; set => SetPropertyValue(ref nickName, value); } string spouseName; public string SpouseName { get => spouseName; set => SetPropertyValue(ref spouseName, value); } TitleOfCourtesy titleOfCourtesy; public TitleOfCourtesy TitleOfCourtesy { get => titleOfCourtesy; set => SetPropertyValue(ref titleOfCourtesy, value); } DateTime? anniversary; public DateTime? Anniversary { get => anniversary; set => SetPropertyValue(ref anniversary, value); } string notes; [FieldSize(4096)] public string Notes { get => notes; set => SetPropertyValue(ref notes, value); } } public enum TitleOfCourtesy { Dr, Miss, Mr, Mrs, Ms }; }
The
Contact
object is inherited from thePerson
class that is part of the Business Class Library. ThePerson
class contains all fields required for personal data records: first name, last name, birth date, and so on.The
Contact
class has the DefaultClassOptionsAttribute attribute. This attribute adds the following functionality:The Contact item appears in the the main page’s navigation control. Users can click this item to access the associated List View.
You can use
Contact
objects as data sources to generate reports (see Create a Report in Visual Studio).
To apply each option separately, use the NavigationItemAttribute and VisibleInReportsAttribute attributes.
To provide a consistent UI, your XAF application must receive notifications from business classes when a user changes the property values of these classes. The notification mechanism uses the standard INotifyPropertyChanged interface and implements its declared PropertyChanged event. In this tutorial, the event is declared in the
Party
ancestor class.To send a notification to the internal XAF code, use the
SetPropertyValue
method to triggerPropertyChanged
from a property set accessor in a business class. TheSetPropertyValue
method checks whether the new property value differs from the previous value. If so, the method stores the previous value and assigns a new value to the property.Refer to the following article for more information: The Importance of Property Change Notifications for Automatic UI Updates.
Go to the MySolution.Module\BusinessObjects\MySolutionDbContext file and add the following properties to the
MySolutionEFCoreDbContext
class:using MySolution.Module.BusinessObjects; namespace MySolution.Module.BusinessObjects { public class MySolutionEFCoreDbContext : DbContext { //... public DbSet<Contact> Contacts { get; set; } public DbSet<Person> Persons { get; set; } public DbSet<Party> Parties { get; set; } public DbSet<Address> Addresses { get; set; } public DbSet<Country> Countries { get; set; } public DbSet<State> States { get; set; } public DbSet<PhoneNumber> PhoneNumbers { get; set; } } }
Each property is a collection of objects of a certain class. For each class, XAF creates a table with the same name in the database and then maps each collection to the relevant table.
If your business class is inherited from non-abstract classes, add the corresponding properties to the DbContext. This way, your application can work with collections of such ancestor classes. For example, in this tutorial, the
Contact
class is inherited from thePerson
class, while thePerson
class is inherited from theParty
class. To use a list of any type from this hierarchy during development, register a property of the same type in the DbContext.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
Your business model is ready. Run the application.
XAF generates a user interface that is based on specified data structures. The List View displays the Contact list:
Click the New button to invoke the (Detail View) that contains editors for each data field. The type of editor depends on the type of value. For example, a date picker for date-time values (see Birthday) and a combo box for enumeration fields (see Title Of Courtesy):
XAF updates form titles, transforms captions from camel-case to space-separated strings, and so on.
Users can rearrange columns, sort and filter data, and use other grid features: