Skip to main content
.NET 6.0+

Session Class

The session that is used to load and save persistent objects.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v23.2.dll

NuGet Package: DevExpress.Xpo

Declaration

public class Session :
    Component,
    ISupportInitialize,
    IPersistentValueExtractor,
    ISessionProvider,
    IObjectLayerProvider,
    IXPDictionaryProvider,
    IDataLayerProvider,
    IWideDataStorage,
    ICommandChannel,
    ICommandChannelAsync,
    IInfrastructure<IServiceProvider>

Remarks

In XPO, Session is a cache of the persistent objects that have been instantiated during data manipulations with a data store. A session uses a Data Access Layer (DAL) to retrieve the persistent objects from a data store and store them. A session automatically creates a DAL for the specified connection properties (such as the database name, user name and password).

Session vs Unit of Work

You can also use UnitOfWork instead of sessions. When working with sessions you need to save each persistent object individually. While working with Units of Work all the changes made to persistent objects are automatically saved to a data store by making a single method call. This is because it tracks every change to every persistent object.

Create a Session

The following code sample demonstrates how to create a new session with default settings.

// Create a new session with default settings.
Session workSession = new Session();

You can create a session at design time

Default Session

In simple applications, manually creating session objects is not required because most data store-specific actions can be performed by XPO using defaults (see XpoDefault.Session or Session.DefaultSession). If you are planning to use multiple data stores simultaneously or share the same Data Access Layer between multiple sessions, then sessions are the cornerstone of your application development.

All the default settings for the default sessions being created can be customized using the XpoDefault object.

Customize a Session

To associate a session with a particular data store and override the defaults, you need to specify the connection specifics via the Session.ConnectionString or Session.Connection properties.

When a connection is not specified, the session uses its default settings to connect to a data store.

See also: Connect to a Data Store

Work with Sessions

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 (for example, 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) {}
}

Connect and Disconnect 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.

The connection is alive while a session is active, i.e., until the Session.Disconnect method is explicitly called. Disconnect is implicitly called when a Session object is disposed of.

Session allows you to

Create a Session at Design Time

In WinForms applications, you can create the Session object at design time:

  1. From the DX.23.2: ORM Components tab of the Toolbox, drag a Session object onto your form or component.

  2. To rename the session object, set its Name property.

  3. To change the visibility level of the session object, set its Modifiers property.

  4. You can specify session options, such as the Session.AutoCreateOption and the Session.LockingOption, at design time. Set the corresponding properties in the Properties window and change the AutoCreateOption.DatabaseAndSchema and the LockingOption.Optimistic property values which are set by default.

  5. You can also handle session-specific events. See the Events tab.

Implements

See Also