Skip to content

Latest commit

 

History

History
117 lines (111 loc) · 7.2 KB

create-persistent-classes-and-connect-xpo-to-database.md

File metadata and controls

117 lines (111 loc) · 7.2 KB

Create persistent classes and connect XPO to the database

    Back to TOC     Step 2

  • Create a new Windows Forms App project in Visual Studio.

  • Add references to the following assemblies (Manage references in a project):
    DevExpress.Data
    DevExpress.Xpo
    These assemblies are available under the Assemblies > Extensions category in the Reference Manager.

  • Create the DataAccess folder and add the Customer class to it. This class is a model for the Customer table in a database.

  • Change the base class to XPObject and add a constructor with a session parameter:

    using DevExpress.Xpo;
    
    namespace XpoTutorial {
        public class Customer :XPObject {
            public Customer(Session session) : base(session) { }
        }
    }

    See also:
    XPO Classes Comparison

  • Add the FirstName and LastName properties to the Customer class. XPO maps these properties to columns in the Customer table.

    private string fFirstName;
    public string FirstName {
        get { return fFirstName; }
        set { SetPropertyValue(nameof(FirstName), ref fFirstName, value); }
    }
    
    private string fLastName;
    public string LastName {
        get { return fLastName; }
        set { SetPropertyValue(nameof(LastName), ref fLastName, value); }
    }

    The SetPropertyValue method raises the PropertyChanged event that is used by data-bound controls to update their display text.

  • Add the read-only ContactName property. XPO does not map read-only properties to database columns. Use the FirstName and LastName properties to calculate the ContactName property value.

    public string ContactName {
        get { return string.Concat(FirstName, " ", LastName); }
    }
  • Add the PersistentAlias attribute to the ContactName property.

    The PersistentAlias attribute specifies an expression that XPO should use instead of the property name in a SQL query. Use Criteria Language to build the expression.

    You can calculate the property value directly (as shown above) or use the EvaluateAlias method. Both approaches are correct. The first approach is preferable for heavy calculations because C# or VB.NET provides many ways for performance optimization.

    [PersistentAlias("Concat([FirstName], ' ', [LastName])")]
    public string ContactName {
        get { return (string)EvaluateAlias(nameof(ContactName)); }
    }
  • Use the same approach to add the Order class with the ProductName(String), OrderDate(DateTime), and Freight(decimal) properties. The complete code is available here: Order.cs

    The maximum number of characters for a column which XPO creates for a property of the String type is 100 (SizeAttribute.DefaultStringMappingFieldSize). Use the Size attribute to change the maximum number of characters for some properties.

  • Add the Size attribute to the ProductName property.

    [Size(200)]
    public string ProductName {
        get { return fProductName; }
        set { SetPropertyValue(nameof(ProductName), ref fProductName, value); }
    }
  • Add the Orders and Customer properties to create a relationship between the Customer and Order classes.

    // Customer.cs
    [Association("CustomerOrders")]
    public XPCollection<Order> Orders {
        get { return GetCollection<Order>(nameof(Orders)); }
    }
    
    // Order.cs
    private Customer fCustomer;
    [Association("CustomerOrders")]
    public Customer Customer {
        get { return fCustomer; }
        set { SetPropertyValue(nameof(Customer), ref fCustomer, value); }
    }
  • Add a reference to the System.Configuration assembly. This assembly contains classes used to access connection string settings from the application's configuration file Connection Strings and Configuration Files.

  • Add the (ConnectionHelper.cs) file to your project.

    This code initializes the Data Access Layer. Refer to the Connecting to a Data Store article for more information.

  • Open the Program.cs file and add the ConnectionHelper.Connect method call to the Main method.

    Visual Studio does not create the Program.vb file in a VB.NET project. You can add this code to the Form1 class constructor before the InitializeComponent method.

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        ConnectionHelper.Connect(false);
        Application.Run(new MainForm());
    }
  • Open the application configuration file (app.config) and add the connection string.

    <connectionStrings>
        <add name="XpoTutorial" connectionString="XpoProvider=InMemoryDataStore"/>
    </connectionStrings>

    The InMemoryDataStore provider is for demo and testing purposes. You can use your database server or an embedded database (for example, SQLite). XPO supports 14 database engines. Refer to the following articles for more information:
    Database Systems Supported by XPO
    K18445 - How to create a correct connection string for XPO providers

  • To populate the database with the demo data, add the DemoDataHelper.cs file to your project and put the following code after the ConnectionHelper.Connect method call.

    using DevExpress.Xpo;
    // ..
    using (UnitOfWork uow = new UnitOfWork()) {
        DemoDataHelper.Seed(uow);
    }

    Back to TOC     Step 2