Skip to main content
.NET 6.0+

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

  • 2 minutes to read

This topic describes how to create, set up, 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 connect to the XAF database and create an Object Space in a non-XAF application.

View Example: XAF - How to create and setup an XtraReport report for exporting to a Stream in a non-XAF application

  1. Implement the IObjectSpaceProviderFactory interface.

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Core;
    using DevExpress.ExpressApp.EFCore;
    using ExportReportEF.Module.BusinessObjects;
    using Microsoft.EntityFrameworkCore;
    
    namespace MyApp // Note: actual namespace depends on the project name.
    {
        class CustomObjectSpaceProviderFactory : IObjectSpaceProviderFactory {
            const string connectionString = @"Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\mssqllocaldb;Initial Catalog=ExportReportEF";
            public IEnumerable<IObjectSpaceProvider> CreateObjectSpaceProviders() {
                return new IObjectSpaceProvider[] {
                    new EFCoreObjectSpaceProvider<ExportReportEFEFCoreDbContext>((builder, _) => { builder.UseSqlServer(connectionString).UseChangeTrackingProxies(); })
                };
            }
        }
    }
    
  2. Create the ServiceCollection to register XAF services and your CustomObjectSpaceProviderFactory service (the Program.cs file).

    internal class Program {
        static void Main(string[] args) {
        // ...
            ServiceCollection serviceDescriptors = new ServiceCollection();
            serviceDescriptors.AddXafCore();
            serviceDescriptors.AddXafReportingCore();
            serviceDescriptors.AddScoped<IObjectSpaceProviderFactory, CustomObjectSpaceProviderFactory>();
    
            IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
            // ...
        }
        // ...
    }
    
  3. Register the required types in the Types Info Subsystem.

    internal class Program {
        static void Main(string[] args) {
        // ...
            var typesInfo = serviceProvider.GetRequiredService<ITypesInfo>();
            InitTypesInfo(typesInfo, serviceProvider.GetRequiredService<IObjectSpaceProviderFactory>());
            RegisterBOTypes(typesInfo);
            // ...
        }
        static void InitTypesInfo(ITypesInfo typesInfo, IObjectSpaceProviderFactory osProviderFactory) {
            var osProviders = osProviderFactory.CreateObjectSpaceProviders();
            osProviders.ForEach(osProvider => ((TypesInfo)typesInfo).AddEntityStore(osProvider.EntityStore));
        }
        static void RegisterBOTypes(ITypesInfo typesInfo) {
            typesInfo.RegisterEntity(typeof(Employee));
        }
    }
    
  4. Use the IReportExportService service to load and prepare a report.

    internal class Program {
        static void Main(string[] args) {
        // ...
            IReportExportService reportExportService = serviceProvider.GetRequiredService<IReportExportService>();
            using var report = reportExportService.LoadReport<ReportDataV2>(data => data.DisplayName == "Employees Report");
            reportExportService.SetupReport(report);
            // ...
        }
        // ...
    }