Server Mode: Binding to a Data Source Using XPO
- 3 minutes to read
To enable a server mode (regular or Instant Feedback) you need to create an appropriate data source. You can use data sources provided by the eXpress Persistent Objects (XPO) library or use dedicated data sources tailored to work with ‘LINQ to SQL Classes’. This topic shows how to use data sources provided by the XPO library.
In this topic, the term grid control refers to a data-aware control that supports a server mode.
Server Mode: Binding to a Data Source Using eXpress Persistent Objects
To implement a server mode using data sources provided by the eXpress Persistent Objects (XPO) library, you should do the following.
Create an XPServerCollectionSource or XPInstantFeedbackSource object. At design time, you can find these components in the Toolbox and add them to the form.
If you need to use the regular (synchronous) server mode, use the XPServerCollectionSource or XPServerModeView component. To use the Instant Feedback (asynchronous) mode, use the XPInstantFeedbackSource or XPInstantFeedbackView component. See the Large Data Sources: Server and Instant Feedback Modes to learn more.
- Provide descriptive information on the target data.
- Assign the created data source to the grid control’s DataSource property.
- Provide connection settings to the target data store.
Note
In a server mode, a target data table must contain a key field. If the table doesn’t contain a key field, it cannot be used as a data source in a server mode.
Providing Descriptive Information on the Target Data
When working with XPO, you need to provide descriptive information on the target data for a server mode data source you are using. To accomplish this, use the following approach.
In code, create a persistent class derived from the XPBaseObject or its descendant that will correspond to the target data table (data view) in the data store. Assign the class’ type to the XPServerCollectionSource.ObjectClassInfo or XPInstantFeedbackSource.ObjectType property. Building the project after creating the class allows you to initialize these properties at design time.
For information on creating persistent classes, please refer to the Basics of Creating Persistent Objects for Existing Data Tables topic.
Providing Connection Settings
You need to provide connection settings to enable XPO to connect to a required data store. There are multiple methods to provide connection settings:
- specify a connection string via the XpoDefault.ConnectionString property;
- specify a connection string via the Session.ConnectionString property of the default Session;. the default Session can be accessed via the static Session.DefaultSession object;
- specify a connection string via the Session.ConnectionString property of a specific Session, UnitOfWork or ExplicitUnitOfWork object; the specified session or unit of work needs to be bound to the XPServerCollectionSource.Session property;
- specify a IDbConnection object that provides connection settings via the Session.Connection property; you can assign this object to the default Session or a specific session/unit of work.
- create a Data Access Layer object, with required connection settings.
The following code shows how to provide connection settings to the NorthWind database stored on the local MS SQLServer. The connection string is generated by the static MSSqlConnectionProvider.GetConnectionString method, which takes the names of the server and database, user name and password as parameters.
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
XpoDefault.ConnectionString = MSSqlConnectionProvider.GetConnectionString(
"(local)", "sa", "", "NorthWind");
See the Grid Server Mode and Grid Instant Feedback UI demos for complete examples of implementing server modes.