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

Inherit from the Business Class Library Class (XPO)

  • 6 minutes to read

In this lesson, you will learn how to implement business classes for your application using the Business Class Library. This library contains the most typical ready-to-use business classes. You will implement a custom Contact class by deriving from the Person class available in this library, and implement several additional properties. You will also learn the basics of automatic user interface construction based on data.

  • Typically, business classes should be implemented in a platform-independent module project, so that the same objects will be available in both WinForms and ASP.NET applications. To simplify the implementation of XAF-specific classes, several Visual Studio templates are supplied. In this lesson, you will use the XPO Business Object template to implement a persistent business class. Right-click the Business Objects folder in the MySolution.Module project, and choose Add DevExpress Item | New Item… to invoke Template Gallery.Then select the XAF Business Object | XPO Business Object template, specify Contact.cs as the new item’s name and press Add Item. As a result, you will get an automatically generated code file with a single class declaration.

    TemplateGalery_XPO

    The auto-generated Contact class is a BaseObject class descendant, which is one of the base persistent classes. You should inherit one of these classes when implementing a persistent class from scratch is required, or use Business Class Library classes (which are derived from the BaseObject class as well). For a general overview of the business class concept, refer to the Business Classes vs Database Tables topic.

  • Replace the automatically generated class declaration with the following code.

    [DefaultClassOptions]
    public class Contact : Person {
        public Contact(Session session) : base(session) { }
        private string webPageAddress;
        public string WebPageAddress {
            get { return webPageAddress; }
            set { SetPropertyValue("WebPageAddress", ref webPageAddress, value); }
        }
        private string nickName;
        public string NickName {
            get { return nickName; }
            set { SetPropertyValue("NickName", ref nickName, value); }
        }
        private string spouseName;
        public string SpouseName {
            get { return spouseName; }
            set { SetPropertyValue("SpouseName", ref spouseName, value); }
        }
        private TitleOfCourtesy titleOfCourtesy;
        public TitleOfCourtesy TitleOfCourtesy {
            get { return titleOfCourtesy; }
            set { SetPropertyValue("TitleOfCourtesy", ref titleOfCourtesy, value); }
        }
        private DateTime anniversary;
        public DateTime Anniversary {
            get { return anniversary; }
            set { SetPropertyValue("Anniversary", ref anniversary, value); }
        }
        private string notes;
        [Size(4096)]
        public string Notes {
            get { return notes; }
            set { SetPropertyValue("Notes", ref notes, value); }
        }
    }
    public enum TitleOfCourtesy { Dr, Miss, Mr, Mrs, Ms };
    

    As you can see, the Contact class ancestor is changed from BaseObject to Person, and several custom properties are implemented. Note that the Contact constructor and the SetPropertyValue method is used in property setters. These are specifics of the eXpress Persistent Objects (XPO) object-relational mapper utilized by XAF. You can refer to the XPO Best Practices knowledge base article and the Simplified Property Syntax topic in the XPO documentation for details.

    Note the use of the DefaultClassOptionsAttribute attribute. In this tutorial, this attribute means that the following capabilities will be available for the Contact business class.

    • The Contact item will be added to the main form’s navigation control. When clicking this item, a List View will be displayed. This List View represents a list of objects of the Contact type.
    • The Contact item will be added to the submenu of the New (new_dropdown_btn) button when objects of another type are displayed in the List View. Click this item to invoke a Contact detail form and create a new Contact object.
    • The Contact objects will be provided as a data source to generate reports (see Create a Report in Visual Studio).

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

    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. There will be a navigation control allowing you to display the Contact list. You will be able to customize this collection using the corresponding editors. If you click the New button or double-click an existing record, the application will show a detail form (Detail View) filled with editors for each data field.

    The following image demonstrates the Detail and List Views in the WinForms application.

    Tutorial_BMD_Lesson2_2

    Notice that many elements have been generated in an intuitive manner in very little time. The proper editors are created for data fields, and appropriate editors are used in the grid controls to display data. Note that a combo box editor has been created for Title Of Courtesy (an enumerator). Also note that captions have automatically been transformed from camel-case to space-separated strings, form titles are automatically updated, etc.

    You can use the grid features to show, hide and rearrange columns, and apply grouping, filtering and sorting to a List View at runtime. In the WinForms application, you can customize the editor layout on the detail form as you like at runtime. For this purpose, right-click an empty space and select Customize Layout. You can now move editors to the required positions. To learn how to customize the editor layout at design time, refer to the Customize the View Items Layout topic. Additionally, you can refer to the View Items Layout Customization and List View Column Generation topics to see how the default Detail View layout and default List View column set are generated.

You can see the code demonstrated here 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.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version of this demo is available online at https://demos.devexpress.com/XAF/MainDemo.

 

Next Lesson: Supply Initial Data (XPO)

See Also