The requested page is not available for the requested platform. You are viewing the content for Default platform.

Access XAF Application Data in a non-XAF Application

  • 3 min 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 described here in XAF applications. Instead, always use the XafApplication.CreateObjectSpace method or access an existing Object Space.

In a non-XAF application, you have no XafApplication object to create an Object Space. But, an XafApplication does not create Object Spaces itself. 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. In XPO-based applications, use the DevExpress.ExpressApp.Xpo.XPObjectSpaceProvider constructor for this purpose. In Entity Framework applications, use the DevExpress.ExpressApp.EF.EFObjectSpaceProvider. Then, you can call the provider's CreateObjectSpace method to create an Object Space.

Entity Framework Example

You can use the following code to access data of the EFDemo application (typically installed to %PUBLIC%\Documents\DevExpress Demos 19.1\Components\eXpressApp Framework\EFDemoCodeFirst) and write the list of Departments to the standard output stream.

using DevExpress.ExpressApp.EF;
using DevExpress.ExpressApp;
using EFDemo.Module.Data;
// ...
class Program {
    static void Main(string[] args) {
        EFObjectSpaceProvider osProvider = new EFObjectSpaceProvider(typeof(EFDemoDbContext),
            "integrated security=True;multipleactiveresultsets=True;data source=(localdb)\\v11.0;initial catalog=EFDemo_");
        XafTypesInfo.Instance.RegisterEntity(typeof(Department));
        IObjectSpace objectSpace = osProvider.CreateObjectSpace();
        foreach (Department department in objectSpace.GetObjects<Department>()) {
           Console.WriteLine(department.Title + "\t" + department.Office);
        }
    }
}

XPO Example

In XPO-based applications, you should additionally initialize the Types Info Subsystem. The following example demonstrates how to access data of the MainDemo application (typically installed to %PUBLIC%\Documents\DevExpress Demos 19.1\Components\eXpressApp Framework\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)\v11.0;initial catalog=MainDemo_", null);
        IObjectSpace objectSpace = osProvider.CreateObjectSpace();
        foreach (Department department in objectSpace.GetObjects<Department>()) {
            Console.WriteLine(department.Title + "\t" + department.Office);
        }
    }
}

If you need 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