Skip to main content
A newer version of this page is available. .
All docs
V22.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. In the MySolution.Module\Business Objects folder, create the Position class. Replace the generated class declaration with the following code:

    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.Validation;
    using System.ComponentModel;
    
    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));
            }
        }
    }
    

    The Position class has the DefaultProperty attribute. This attribute specifies the default property of the class. Use this attribute to specify the most descriptive property of your class. The default 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.

  2. Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of Position type:

    public class MySolutionEFCoreDbContext : 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 the property has the virtual access modifier for lazy loading implementation. For additional information, refer to Microsoft documentation: Lazy Loading.

    To notify the application of changes a user makes to this property, use the SetReferencePropertyValue method. This method triggers the property change event implemented in the Position class the same way as the SetPropertyValue method that you used in Inherit from the Business Class Library Class (EF Core).

  4. Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.

  5. Run the application.

    You can see that the navigation control displays the Position item. Click this item to access a position list. Click New to open the position detail form:

    xaf ASP.NET Core Blazor detail view

  6. Use the detail form to create Developer and Manager positions. Now they appear in the position list:

    xaf ASP.NET Core Blazor list view

  7. Open the Contact Detail View. In this view, XAF creates a lookup editor for the Position field. Lookup editors support incremental filtering. This editor uses a special type of View - Lookup List View. The Lookup List View includes a single column that displays values of the class default property. In your application, these are the values of the Title property, that you created in the previous step. Users can select Position values from the dropdown lists.

    xaf ASP.NET Core Blazor detail view lookup editor

    For additional information on editors that display reference properties refer to Reference (Foreign Key, Complex Type) Properties.

Next Lesson

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

See Also