How to: Use XAF Reports in a non-XAF Application

This topic describes how to create, setup and export XAF reports in a non-XAF application. Since XAF stores reports in the database, and XAF reports use Object Space to retrieve data, you should manually connect to the XAF database and create an Object Space in a non-XAF application.

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T275059.

  1. Implement the IReportObjectSpaceProvider and IObjectSpaceCreator interfaces.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.ReportsV2;
    using DevExpress.Persistent.Base.ReportsV2;
    // ...
    public class MyReportObjectSpaceProvider : IReportObjectSpaceProvider, IObjectSpaceCreator {
        IObjectSpaceProvider objectSpaceProvider;
        IObjectSpace objectSpace;
        public MyReportObjectSpaceProvider(IObjectSpaceProvider objectSpaceProvider)  {
            this.objectSpaceProvider = objectSpaceProvider;
        }
        public void DisposeObjectSpaces() {
            if (objectSpace != null) {
                objectSpace.Dispose();
                objectSpace = null;
            }
        }
        public IObjectSpace GetObjectSpace(Type type) {
            if (objectSpace == null) {
                objectSpace = objectSpaceProvider.CreateObjectSpace();
            }
            return objectSpace;
        }
        public IObjectSpace CreateObjectSpace(Type type) {
            return objectSpaceProvider.CreateObjectSpace();
        }
    }
    
  2. Inherit the ReportDataSourceHelper class and override the CreateReportObjectSpaceProviderCore method.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.ReportsV2;
    using DevExpress.Persistent.Base.ReportsV2;
    // ...
    public class MyReportDataSourceHelper : ReportDataSourceHelper {
        IObjectSpaceProvider objectSpaceProvider;
        public MyReportDataSourceHelper(IObjectSpaceProvider objectSpaceProvider) : base(null) {
            this.objectSpaceProvider = objectSpaceProvider;
        }
        protected override IReportObjectSpaceProvider CreateReportObjectSpaceProvider() {
            return new MyReportObjectSpaceProvider(objectSpaceProvider);
        }
    }
    
  3. Create an Object Space Provider using the approach from the Access XAF Application Data in a non-XAF Application article and register the required types in the Types Info Subsystem.

    An example for the Entity Framework.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.DC;
    using DevExpress.ExpressApp.EF;
    using DevExpress.Persistent.BaseImpl.EF;
    // ...
    connectionString = 
        @"Integrated Security=SSPI;Pooling=false;MultipleActiveResultSets=True;Data Source=(localdb)\v11.0;Initial Catalog=ExportReportDemoEF";
    EFObjectSpaceProvider objectSpaceProvider = new EFObjectSpaceProvider(typeof(ExportReportDemoEFDbContext), connectionString);
    ((TypesInfo)XafTypesInfo.Instance).AddEntityStore(objectSpaceProvider.EntityStore);
    XafTypesInfo.Instance.RegisterEntity(typeof(Person));
    

    An example for XPO.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.DC;
    using DevExpress.ExpressApp.DC.Xpo;
    using DevExpress.ExpressApp.Xpo;
    using DevExpress.Persistent.BaseImpl;
    // ...
    connectionString =
        @"Integrated Security=SSPI;Data Source=(localdb)\v11.0;Initial Catalog=ExportReportDemo";
    XpoTypesInfoHelper.ForceInitialize();
    ITypesInfo typesInfo = XpoTypesInfoHelper.GetTypesInfo();
    XpoTypeInfoSource xpoTypeInfoSource = XpoTypesInfoHelper.GetXpoTypeInfoSource();
    typesInfo.RegisterEntity(typeof(Person));
    typesInfo.RegisterEntity(typeof(ReportDataV2));
    ConnectionStringDataStoreProvider dataStoreProvider = new ConnectionStringDataStoreProvider(connectionString);
    XPObjectSpaceProvider objectSpaceProvider = new XPObjectSpaceProvider(dataStoreProvider, typesInfo, xpoTypeInfoSource);
    
  4. Use the approach described in the How to: Print a Report Without Displaying a Preview topic to access and print the report.

    using DevExpress.ExpressApp;
    using DevExpress.XtraReports.UI;
    using DevExpress.ExpressApp.ReportsV2;
    // ...
    IObjectSpace objectSpace = objectSpaceProvider.CreateObjectSpace();
    ReportDataV2 reportData = objectSpace.FindObject<ReportDataV2>(
        new BinaryOperator("DisplayName", "Employees Report"));
    XtraReport report = ReportDataProvider.ReportsStorage.LoadReport(reportData);
    MyReportDataSourceHelper reportDataSourceHelper = new MyReportDataSourceHelper(objectSpaceProvider);
    ReportDataProvider.ReportObjectSpaceProvider = new MyReportObjectSpaceProvider(objectSpaceProvider);
    reportDataSourceHelper.SetupBeforePrint(report);
    report.ExportToPdf("report.pdf");