Skip to main content

Access XAF Application Data in a non-XAF Application

  • 3 minutes to read

In certain scenarios, you may need to create an auxiliary application for database maintenance and use Object Space to query your primary XAF application data. This topic describes how you can create and use Object Space in a regular non-XAF application.

Important

We do not recommend using the approach from this topic in XAF applications. Instead, access an Object Space with context-dependent options. For more information, refer to the following topic: Ways to Access an Object Space (the Database Context for CRUD Operations).

In a non-XAF application, there is no XafApplication object with which to create an Object Space. An XafApplication itself does not create Object Spaces. Internally, it uses the Object Space Provider designed to create Object Spaces for the currently used ORM and those registered in its overridden XafApplication.CreateDefaultObjectSpaceProvider method. In a non-XAF application, you can instantiate the Object Space Provider manually and then call the provider’s CreateObjectSpace method to create an Object Space.

ORM

Object Space Provider

XPO

XPObjectSpaceProvider / SecuredObjectSpaceProvider

Entity Framework Core

EFCoreObjectSpaceProvider<TDbContext> / SecuredEFCoreObjectSpaceProvider<TDbContext>

Note

If you want to use the Security System in your non-XAF application, refer to the following help topic: Use the Security (Access Control & Authentication) API in Non-XAF Applications - Free Offer.

XPO Example

In XPO-based applications, the Types Info Subsystem should be initialized. The following example demonstrates how to access MainDemo application data (typically installed to %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\MainDemo) and write the list of Departments to the standard output stream:

using DevExpress.ExpressApp.Xpo;
using DevExpress.ExpressApp;
using MainDemo.Module.BusinessObjects;
// ...
class Program {
    static void Main(string[] args) {
        XpoTypesInfoHelper.GetXpoTypeInfoSource();
        XafTypesInfo.Instance.RegisterEntity(typeof(Department));
        XPObjectSpaceProvider osProvider = new XPObjectSpaceProvider(
        @"integrated security=SSPI;pooling=false;data source=(localdb)\MSSQLLocalDB;initial catalog=MainDemo_", null);
        IObjectSpace objectSpace = osProvider.CreateObjectSpace();
        foreach (Department department in objectSpace.GetObjects<Department>()) {
            Console.WriteLine(department.Title + "\t" + department.Office);
        }
    }
}

Entity Framework Core Example

Use the following code snippet to access EFCore console or WinForms application data, and write the list of Departments to the standard output stream.

var objectSpaceProvider = new EFCoreObjectSpaceProvider<ApplicationDbContext>(
     (builder, _) => builder
        .UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=EFCoreTestDB;Integrated Security=True;MultipleActiveResultSets=True")
        .UseLazyLoadingProxies()
        .UseChangeTrackingProxies()
  );
XafTypesInfo.Instance.RegisterEntity(typeof(Department));
IObjectSpace objectSpace = objectSpaceProvider.CreateObjectSpace();
foreach (Department department in objectSpace.GetObjects<Department>()) {
    Console.WriteLine(department.Title + "\t" + department.Office);
}

The following example demonstrates how you can access XAF data in non-XAF applications using the XAF Security System: Role-based Access Control, Permission Management, and OData / Web / REST API Services for Entity Framework and XPO ORM.

Note

To populate the database with initial data, use the DatabaseUpdater.Update method as follows:

using DevExpress.ExpressApp.Updating;
// ...
DatabaseUpdater databaseUpdater = new DatabaseUpdater(
    osProvider, new ModuleBase[0], "", osProvider.ModuleInfoType);
databaseUpdater.Update();
See Also