Skip to main content
A newer version of this page is available. .

Define the Data Model and Set the Initial Data

  • 8 minutes to read

This topic describes how to define the data model and business logic for your Blazor application. The application’s data model contains two logical parts:

  • Marketing: the Customer and Testimonial classes
  • Planning: the Project and Task classes

Add the Customer and Testimonial Persistent Classes

  1. In the Solution Explorer, right-click the SimpleProjectManager.Module\BusinessObjects folder and select Add | Class… from the context menu. Set the name to “Marketing” and click Add.

  2. Replace the auto-generated file’s content with the following code:

    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.BaseImpl;
    using DevExpress.Xpo;
    using System;
    
    namespace SimpleProjectManager.Module.BusinessObjects.Marketing {
        [NavigationItem("Marketing")]
        public class Customer : BaseObject {
            public Customer(Session session) : base(session) { }
            string firstName;
            public string FirstName {
                get { return firstName; }
                set { SetPropertyValue(nameof(FirstName), ref firstName, value); }
            }
            string lastName;
            public string LastName {
                get { return lastName; }
                set { SetPropertyValue(nameof(LastName), ref lastName, value); }
            }
            string email;
            public string Email {
                get { return email; }
                set { SetPropertyValue(nameof(Email), ref email, value); }
            }
            string company;
            public string Company {
                get { return company; }
                set { SetPropertyValue(nameof(Company), ref company, value); }
            }
            string occupation;
            public string Occupation {
                get { return occupation; }
                set { SetPropertyValue(nameof(Occupation), ref occupation, value); }
            }
            [Association("Customer-Testimonials")]
            public XPCollection<Testimonial> Testimonials {
                get { return GetCollection<Testimonial>(nameof(Testimonials)); }
            }
            public string FullName {
                get {
                    string namePart = string.Format("{0} {1}", FirstName, LastName);
                    return Company != null ? string.Format("{0} ({1})", namePart, Company) : namePart;
                }
            }
            byte[] photo;
            [ImageEditor(ListViewImageEditorCustomHeight = 75, DetailViewImageEditorFixedHeight = 150)]
            public byte[] Photo {
                get { return photo; }
                set { SetPropertyValue(nameof(Photo), ref photo, value); }
            }
        }
        [NavigationItem("Marketing")]
        public class Testimonial : BaseObject {
            public Testimonial(Session session) : base(session) {
                createdOn = DateTime.Now;
            }
            string quote;
            public string Quote {
                get { return quote; }
                set { SetPropertyValue(nameof(Quote), ref quote, value); }
            }
            string highlight;
            [Size(512)]
            public string Highlight {
                get { return highlight; }
                set { SetPropertyValue(nameof(Highlight), ref highlight, value); }
            }
            DateTime createdOn;
            [VisibleInListView(false)]
            public DateTime CreatedOn {
                get { return createdOn; }
                internal set { SetPropertyValue(nameof(CreatedOn), ref createdOn, value); }
            }
            string tags;
            public string Tags {
                get { return tags; }
                set { SetPropertyValue(nameof(Tags), ref tags, value); }
            }
            Customer customer;
            [Association("Customer-Testimonials")]
            public Customer Customer {
                get { return customer; }
                set { SetPropertyValue(nameof(Customer), ref customer, value); }
            }
        }
    }
    
    Show the API description

    API

    Description

    BaseObject

    The base class that provides features for business classes.

    NavigationItemAttribute

    Places a navigation item created for the decorated class in the specified navigation group.

    AssociationAttribute

    Specifies that the decorated property is part of an association.

    ImageEditorAttribute

    Specifies the width and high of the editor that displays an image property.

    SizeAttribute

    Specifies the maximum number of characters that the decorated member’s editor can contain.

    VisibleInListViewAttribute

    Shows or hides a column with the decorated property’s values in an initial List View.

Add the Project and ProjectTask Persistent Classes

  1. In the Solution Explorer, right-click the SimpleProjectManager.Module\BusinessObjects folder and select Add | Class… from the context menu. Set the name to “Planning” and click Add.

  2. Replace the auto-generated file’s content with the following code:

    using System;
    using DevExpress.Persistent.Base;
    using DevExpress.Persistent.BaseImpl;
    using DevExpress.Xpo;
    
    namespace SimpleProjectManager.Module.BusinessObjects.Planning {
        [NavigationItem("Planning")]
        public class ProjectTask : BaseObject {
            public ProjectTask(Session session) : base(session) { }
            string subject;
            [Size(255)]
            public string Subject {
                get { return subject; }
                set { SetPropertyValue(nameof(Subject), ref subject, value); }
            }
            ProjectTaskStatus status;
            public ProjectTaskStatus Status {
                get { return status; }
                set { SetPropertyValue(nameof(Status), ref status, value); }
            }
            Person assignedTo;
            public Person AssignedTo {
                get { return assignedTo; }
                set { SetPropertyValue(nameof(AssignedTo), ref assignedTo, value); }
            }
            DateTime startDate;
            public DateTime StartDate {
                get { return startDate; }
                set { SetPropertyValue(nameof(startDate), ref startDate, value); }
            }
            DateTime endDate;
            public DateTime EndDate {
                get { return endDate; }
                set { SetPropertyValue(nameof(endDate), ref endDate, value); }
            }
            string notes;
            [Size(SizeAttribute.Unlimited)]
            public string Notes {
                get { return notes; }
                set { SetPropertyValue(nameof(Notes), ref notes, value); }
            }
            Project project;
            [Association]
            public Project Project {
                get { return project; }
                set { SetPropertyValue(nameof(Project), ref project, value); }
            }
        }
        [NavigationItem("Planning")]
        public class Project : BaseObject {
            public Project(Session session) : base(session) { }
            string name;
            public string Name {
                get { return name; }
                set { SetPropertyValue(nameof(Name), ref name, value); }
            }
            Person manager;
            public Person Manager {
                get { return manager; }
                set { SetPropertyValue(nameof(Manager), ref manager, value); }
            }
            string description;
            [Size(SizeAttribute.Unlimited)]
            public string Description {
                get { return description; }
                set { SetPropertyValue(nameof(Description), ref description, value); }
            }
            [Association, Aggregated]
            public XPCollection<ProjectTask> Tasks {
                get { return GetCollection<ProjectTask>(nameof(Tasks)); }
            }
        }
    
        public enum ProjectTaskStatus {
            NotStarted = 0,
            InProgress = 1,
            Completed = 2,
            Deferred = 3
        }
    }
    
    Show the API description

    API

    Description

    BaseObject

    The base class that provides features for business classes.

    NavigationItemAttribute

    Places a navigation item automatically created for the decorated class in the specified navigation group.

    AssociationAttribute

    Specifies that the decorated property is a part of an association.

    AggregatedAttribute

    Specifies that the decorated property is an aggregated part of an association.

    SizeAttribute

    Specifies the maximum number of characters that the decorated member’s editor can contain.

Populate the Database with Initial Data

Open the Updater.cs file from the SimpleProjectManager.Module project’s Database Update folder and override the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method as shown below:

using DevExpress.Persistent.BaseImpl;
using SimpleProjectManager.Module.BusinessObjects.Marketing;
using SimpleProjectManager.Module.BusinessObjects.Planning;
// ...
public class Updater : ModuleUpdater {
    //...
    public override void UpdateDatabaseAfterUpdateSchema() {
        base.UpdateDatabaseAfterUpdateSchema();
        if (ObjectSpace.CanInstantiate(typeof(Person))) {
            Person person = ObjectSpace.FindObject<Person>(
                CriteriaOperator.Parse("FirstName == ? && LastName == ?", "John", "Nilsen"));
            if (person == null) {
                person = ObjectSpace.CreateObject<Person>();
                person.FirstName = "John";
                person.LastName = "Nilsen";
            }
        }
        if (ObjectSpace.CanInstantiate(typeof(ProjectTask))) {
            ProjectTask task = ObjectSpace.FindObject<ProjectTask>(
                new BinaryOperator("Subject", "TODO: Conditional UI Customization"));
            if (task == null) {
                task = ObjectSpace.CreateObject<ProjectTask>();
                task.Subject = "TODO: Conditional UI Customization";
                task.Status = ProjectTaskStatus.InProgress;
                task.AssignedTo = ObjectSpace.FindObject<Person>(
                    CriteriaOperator.Parse("FirstName == ? && LastName == ?", "John", "Nilsen"));
                task.StartDate = new DateTime(2019, 1, 30);
                task.Notes = "OVERVIEW: http://www.devexpress.com/Products/NET/Application_Framework/features_appearance.xml";
            }
        }
        if (ObjectSpace.CanInstantiate(typeof(Project))) {
            Project project = ObjectSpace.FindObject<Project>(
                new BinaryOperator("Name", "DevExpress XAF Features Overview"));
            if (project == null) {
                project = ObjectSpace.CreateObject<Project>();
                project.Name = "DevExpress XAF Features Overview";
                project.Manager = ObjectSpace.FindObject<Person>(
                    CriteriaOperator.Parse("FirstName == ? && LastName == ?", "John", "Nilsen"));
                project.Tasks.Add(ObjectSpace.FindObject<ProjectTask>(
                    new BinaryOperator("Subject", "TODO: Conditional UI Customization")));
            }
        }
        if (ObjectSpace.CanInstantiate(typeof(Customer))) {
            Customer customer = ObjectSpace.FindObject<Customer>(
                CriteriaOperator.Parse("FirstName == ? && LastName == ?", "Ann", "Devon"));
            if (customer == null) {
                customer = ObjectSpace.CreateObject<Customer>();
                customer.FirstName = "Ann";
                customer.LastName = "Devon";
                customer.Company = "Eastern Connection";
            }
        }
        ObjectSpace.CommitChanges();
    }
    //...
}

In the code above, the Object Space is used to create initial data. This is one of the main framework abstractions that allows you to perform CRUD (create-read-update-delete) operations. You can find more information on the ObjectSpace in the next topic (the Define Custom Logic and UI Elements section).

Note

You can refer to Supply Initial Data (XPO/EF) for more information on how to initially populate the database.

Run the Application

Press Start Debugging or the F5 key to run the application.

The following image shows the application’s auto-generated UI:

The Blazor application

XAF generates this UI for List and Detail Views with CRUD operations and other functionality (view, search, etc.). The Detail View contains editors (text box, memo, drop-down box, image and date picker, etc.) that display different business class properties.

Lookup and collection editors display properties that are part of an association. For example, the Project and ProjectTask classes participate in a One-To-Many relationship. The editor for the “Many” part (the Project‘s Tasks property) allows users to add, edit, remove, and export tasks.

To display reference properties (the ProjectTask‘s AssignedTo property), XAF generates a drop-down list of persons in the UI and creates a foreign key that references the Person table in the database. This drop-down list displays a person’s full names because the FullName property is the default property of the Person class (see Default Property of a Business Class).

Note

You can find more information on UI generation in the List View Column Generation and View Items Layout Generation topics.

Auto-Created Database

The database is automatically created based on your data model. The database columns are generated based on the persistence settings specified in the data model (such as field size set via an attribute). The image below shows the Object Explorer window from the SQL Server Management Studio.

Database tables and columns

The applications’ navigation control contains items for each database table. These navigation items allow a user to navigate to a List View with records and open their Detail Views.

Next topic: Customize the Application UI and Behavior

See Also