Skip to main content
All docs
V21.2
.NET Framework 4.5.2+

Inherit from the Business Class Library Class (EF Core)

  • 4 minutes to read

This lesson explains how to use the Business Class Library to implement business classes for your application.

Step-by-Step Instructions

  1. Right-click the Business Objects folder in the MySolution.Module project, and choose Add | Class…. Specify Contact.cs as the new class name and click Add.
  2. Replace the generated class declaration with the following code.

    using System;
    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 };
    }
    
  3. Add all the new classes to the solution’s DbContext. Since Contact is a descendant of Person, the Person class ancestors should also be registered. Edit the BusinessObjects\MySolutionDbContext.cs file as shown below.

    using MySolution.Module.BusinessObjects;
    
    namespace  MySolution.Module.BusinessObjects {
        public class MySolutionDbContext : DbContext {
            //...
            public DbSet<Contact> Contacts { 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; }
        }
    }
    
  4. Your business model is ready, but if you have started the application with another version of DbContext, you will get the following error:

    Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'NickName'.

    You can avoid this error in the following ways:

    • Use Migrations.
    • Drop the database every time you change something in the business model (create a new class, add a new attribute to an existing class, rename a class or an attribute, etc.).
    • Use an in-memory database while developing your application. To do this automatically during the debugging process, comment the UseSqlServer option and uncomment the UseInMemoryDatabase option in the MySolution.Blazor.Server\Startup.cs file.

      namespace MySolution.Blazor.Server {
          public class Startup {
              // ...
              public void ConfigureServices(IServiceCollection services) {
                  // ...
                  services.AddDbContextFactory<MySolutionDbContext>((serviceProvider, options) => {
                      options.UseInMemoryDatabase("InMemory");
                      //string connectionString = Configuration.GetConnectionString("ConnectionString");
                      //options.UseSqlServer(connectionString);
                      // ...
                  }, ServiceLifetime.Scoped);
              }
              // ...
          }
      }
      

      In this tutorial, we will use the in-memory database for development purposes.

  5. Run the application. Use the “Admin” name and empty password as login credentials.

    XAF generates a user interface that is based on specified data structures:

    • List View

      List View displays the Contact list. If users click the New button or click an existing record, the application shows a detail form (Detail View) filled with editors for each data field.

      XAF ASP.NET Core Blazor App List View

    • Detail View

      XAF ASP.NET Core Blazor App Detail View

 

Detailed Explanation

Convention

Typically, business classes do not depend on application UI and should be implemented in a platform-independent module project. Business objects can be shared with other XAF or non-XAF applications.

Add a Business Object

The Contact object derives from the Person class included in the Business Class Library. It contains all fields required for personal data records: first name, last name, birth date, and so on.

To provide a consistent user interface, your XAF application must receive notifications from business classes when their property values are changed. The notification mechanism is based on supporting the standard INotifyPropertyChanged interface and implementing its declared PropertyChanged event. To send a notification to internal XAF code, trigger PropertyChanged from a property set accessor within a business class.

Refer to the following article for more information: The Importance of Property Change Notifications for Automatic UI Updates

Attributes

In this tutorial the business objects class is decorated with the DefaultClassOptionsAttribute attribute. This attribute adds the following functionality to Contact business class.

  • The Contact item is added to the main page’s navigation control. Users can click this item to access the associated List View.

  • Contact objects can be used as data sources to generate reports (see Create a Report in Visual Studio).

To apply each of these options separately, use the NavigationItemAttribute and VisibleInReportsAttribute attributes.

User Interface

XAF generates editors depending on data field types. For example, date pickers for date-time values, and combo box editors for enumeration fields (for instance, Title Of Courtesy).

XAF transforms captions from camel-case to space-separated strings, updates form titles, etc.

Users can rearrange columns, sort and filter data, and use other grid features:

Next Lesson

Supply Initial Data (EF Core)

See Also