Implement Custom Business Classes and Reference Properties (XPO)

In this lesson, you will learn how to implement business classes from scratch. For this purpose, the Department and Position business classes will be implemented. These classes will be used in the Contact class, implemented previously. You will also learn the basics of automatic user interface construction for referenced objects.

Note

Before proceeding, take a moment to review the Inherit from the Business Class Library Class (XPO) lesson.

  • Add the following Department and Position persistent classes to the Contact.cs (Contact.vb) file.

    [DefaultClassOptions]
    [System.ComponentModel.DefaultProperty("Title")]
    public class Department : BaseObject {
        public Department(Session session) : base(session) { }
        private string title;
        public string Title {
            get { return title; }
            set { SetPropertyValue("Title", ref title, value); }
        }
        private string office;
        public string Office {
            get { return office; }
            set { SetPropertyValue("Office", ref office, value); }
        }
    }
    [DefaultClassOptions]
    [System.ComponentModel.DefaultProperty("Title")]
    public class Position : BaseObject {
        public Position(Session session) : base(session) { }
        private string title;
        public string Title {
            get { return title; }
            set { SetPropertyValue("Title", ref title, value); }
        }
    }
    

    These new classes are persistent, as they are BaseObject class descendants.

    Note

    The DefaultProperty attribute is used in the code above. This attribute is used to specify the default property of the class. You can specify the most descriptive property of your class in the DefaultProperty attribute, and its values will be displayed in the following.

    • Detail form captions
    • The leftmost column of a List View
    • The Lookup List Views (These Views will be explained in the last step of this lesson.)

    Refer to the Data Annotations in Data Model topic for additional information.

  • Add the Department and Position properties to the Contact class. The following code demonstrates this.

    [DefaultClassOptions]
    public class Contact : Person {
        //...
        private Department department;
        public Department Department {
            get {return department;}
            set {SetPropertyValue("Department", ref department, value);}
        }
        private Position position;
        public Position Position {
            get {return position;}
            set {SetPropertyValue("Position", ref position, value);}
        }
        //...
    }
    

    The Contact class now exposes the Position and Department reference properties.

    Note

    If you have CodeRush installed, you can use Code Templates when implementing business classes. Using Code Templates decreases code creation time, because it helps avoid having to type the entire code manually and allows you to create regular code sections with only a few keystrokes. To learn about the built-in Code Templates for eXpress Persistent Objects, refer to the XPO and XAF Templates topic.

  • Run the WinForms or ASP.NET application. You will see how the user interface is automatically generated using the specified data structures. The navigation control will contain new Department and Position items, which will allow you to access Department and Position objects. Note that in the Contact Detail View, a lookup editor has been created for Department and Position. In this editor, a special type of View, Lookup List View, is used. Typically, this View has a single column corresponding to the class' default property. Using the lookup editor, you can select the required Department (Position) for the current Contact, and add new Department (Position) objects using the New button. In addition, you will also be able to edit existing Department (Position) objects by holding down SHIFT+CTRL and clicking the selected object.

    Tutorial_BMD_Lesson3_1

You can see the code demonstrated in this lesson in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 18.1\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/.

 

Next Lesson: Add a Class from the Business Class Library (XPO)

See Also