A newer version of this page is available. Switch to the current version.

Working with Sessions

  • 2 minutes to read

Once a session has been created and customized, it can be assigned to the Session property of any object which is provided by XPO and is used to represent data (e.g. XPCollection, XPView).

NOTE

XPO doesn't require you to specify the working session when you are creating an object - the Session.DefaultSession will be treated as the working session.

To work with your own session, you have to pass it to the constructor of your persistent object(s) and call the base constructor with this session as a parameter.


public class Customer: XPObject {
    public string Name {
        get { return fName; }
        set { SetPropertyValue(nameof(Name), ref fName, value); }
    }
    string fName = "";

    public string CompanyName {
        get { return fCompanyName; }
        set { SetPropertyValue(nameof(CompanyName), ref fCompanyName, value); }
    }
    string fCompanyName = "";

    public string Phone {
        get { return fPhone; }
        set { SetPropertyValue(nameof(Phone), ref fPhone, value); }
    }
    string fPhone = "";

    public string Fax {
        get { return fFax; }
        set { SetPropertyValue(nameof(Fax), ref fFax, value); }
    }
    string fFax = "";

    // Specify a session for the Customer object.
    public Customer(Session session) : base(session) {}
}

The session automatically connects to the data store the first time persistent objects are loaded or saved. To explicitly connect to the data store, use the Session.Connect method. If the session is already connected to a data store, the Connect method does nothing.