Skip to main content
A newer version of this page is available. .
All docs
V21.1
.NET Framework 4.5.2+

Implement Custom Business Classes and Reference Properties (EF Core)

  • 3 minutes to read

This lesson explains the following concepts:

  • How to implement business classes from scratch
  • How to implement object references to existing classes
  • How XAF generates UI for referenced classes

Note

Before you proceed, take a moment to review the previous lesson: Inherit from the Business Class Library Class (EF Core)

Step-by-Step Instructions

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

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.Validation;
    
    namespace MySolution.Module.BusinessObjects {
        [DefaultClassOptions]
        [DefaultProperty(nameof(Title))]
        public class Position : INotifyPropertyChanged {
            public Position() {
                Contacts = new List<Contact>();
            }
            int id;
            [Browsable(false)]
            public int ID {
                get => id; protected set {
                    if(id == value) {
                        return;
                    }
    
                    id = value;
                    OnPropertyChanged();
                }
            }
            string title;
            public string Title {
                get => title; set {
                    if(title == value) {
                        return;
                    }
    
                    title = value;
                    OnPropertyChanged();
                }
            }
            IList<Contact> contacts;
            public virtual IList<Contact> Contacts {
                get => contacts; set {
                    if(contacts == value) {
                        return;
                    }
    
                    contacts = value;
                    OnPropertyChanged();
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
  2. Register the new class in DbContext. Edit the BusinessObjects\MySolutionDbContext.cs file as shown below.

    public class MySolutionDbContext : DbContext {
        //...
        public DbSet<Position> Positions { get; set; } 
    }
    
  3. Add the Position property to the Contact class:

    public class Contact : Person {
        //...
        Position position;
        public virtual Position Position {
            get => position;
            set => SetReferencePropertyValue(ref position, value);
        }
    }
    

    The Contact class now exposes the Position reference property. Note that this property is declared as virtual.

  4. Run the application.

    After you run the application, you can see that the navigation control displays the Position item. Click the new item to access a position list.

    • Position detail form:

      xaf ASP.NET Core Blazor detail view

    • Position list:

      xaf ASP.NET Core Blazor list view

    In the Contact Detail View, XAF creates a lookup editor for a Position field. This editor use a special type of View - Lookup List View. Typically, this View includes a single column that displays values from the class’s default property. For more information about default properties, refer to the section below.

    xaf ASP.NET Core Blazor detail view lookup editor

    Users can select Position values from the dropdown lists. Lookup editors support incremental filtering.

Detailed Explanation

Default Property

The Position class is decorated with the DefaultProperty attribute. This attribute specifies the default property of the class. Use this attribute to specify the most descriptive property of your class. This property values are displayed in:

  • Detail form captions
  • The leftmost column of a List View
  • Lookup List Views

Refer to the following topic for more information: Data Annotations in Data Model.

Next Lesson

Set a Many-to-Many Relationship (EF Core)

See Also