Implement Custom Business Classes and Reference Properties implement custom business class and reference attributes (EF)

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

In this lesson, you will learn how to start from scratch to achieve business class. For this purpose, the realization of business class jobs. Such will be used to "Contact" category previously implemented. You will also learn the basics of the user interface automatically construct an object reference.

Note

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

In this lesson, you will learn how to start from scratch to achieve business class. For this purpose, the realization of business class jobs. Such will be used to "Contact" category previously implemented. You will also learn the basics of the user interface automatically construct an object reference.

  • Add the Position class as shown in the Inherit from the Business Class Library Class (EF) lesson. Replace the auto-generated code with the following.

  • Add class positions, as shown in succession from the business class library (EF) of course. The automatically generated code replaced with the following.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using DevExpress.Persistent.Base;
    
    namespace MySolution.Module.BusinessObjects {
        [DefaultClassOptions]
        [DefaultProperty(nameof(Title))]
        public class Position {
            public Position() {
                Contacts = new List<Contact>();
            }
            [Browsable(false)]
            public Int32 ID { get; protected set; }
            public String Title { get; set; }
            public virtual IList<Contact> Contacts { get; set; }
        }
    }

     

    Note that the Contact collection property is declared as virtual.

  • Please note that the collection of contacts property declared as virtual.

  • 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.

    Note
    DefaultProperty property used in the above code. This property is used to specify default properties category. You can specify the class of the most descriptive attributes DefaultProperty property, its value will be displayed in the following content.

    • 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.)
    • Details form title
    • The left-most column of the list view
    • Find a list view (the view will be the last step in the introduction of this lesson.

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

    For more information, see the topic annotation data in the data model.

    Important

    You can use the XAF Business Object | EF Business Object project item template from the Template Gallery to add an entity that supports IXafEntityObject, IObjectSpaceLink and INotifyPropertyChanged interfaces, which may be useful in many scenarios.

  • Register the new class in DbContext. Edit the BusinessObjects\MySolutionDbContext.cs file as shown below.

            Important
           You can use business objects XAF | add business objects EF project item template from the library to add support IXafEntityObject, IObjectSpaceLink and INotifyPropertyChanged interface entity, which may be useful in many cases.

  • Register a new class in the DbContext. _MySolutionDbContext.cs edit business object file, as shown in FIG.

 

  • public class MySolutionDbContext : DbContext {
        //...
        public DbSet<Position> Positions { get; set; } 
    }

     

  • Add the Position property to the Contact class. The following code demonstrates this.

  • Add the "position" property to "Contacts" category. The following code demonstrates this.

    public class Contact : Person {
        //...
        public virtual Position Position { get; set; } 
    }

     

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

           Contact Humanity is now open "position" reference properties. Please note that this property is declared as virtual.

 

  • 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 a new Position item, which will allow you to access Position objects. Note that in the Contact Detail View, a lookup editor has been created for 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 Position for the current Contact, and add new Position objects using the New button. In addition, you will also be able to edit the existing Position object by holding down SHIFT+CTRL and clicking the selected object.

  • Run WinForms or ASP.NET applications. You will see how to use the specified data structures automatically generated user interface. The navigation controls include a new "position" item, which will allow you to access the "position" object. Please note that in the "Contact Details" view, has been created to find editor "position." In this editor, using a special type of view "to find the list view." Typically, this view corresponding to a single row of the class-default properties. Use the Find editor, you can select the location of the current contact, and use the "New" button to add a new "position" object. In addition, you can also click and hold down SHIFT_CTRL selected object to edit an existing position of the object.

    Tutorial_BMD_Lesson3_1(EF)

You can see the code demonstrated in this lesson in the EFDemo.Module | Data | Contact.cs (Contact.vb) and Position.cs (Position.vb) files of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 19.2\Components\eXpressApp Framework\EFDemoCodeFirst.

You can EFDemo module |. Data | Contact.cs (Contact.vb) and Position.cs (position .vb) file installed with EF XAF demo (code first). By default, EF demo (code first) application installed

Guess you like

Origin www.cnblogs.com/foreachlife/p/Implement-Custom-Business-Classes-and-Reference-Properties.html