This example illustrates how to access an XAF report stored in the database and populate it with data in a non-XAF application.
Since XAF stores reports in the database and XAF reports use Object Spaces to retrieve data from the database, it is necessary to connect to the XAF database and create an Object Space to use XAF reports in a non-XAF application.
Implementation Details
- Create a custom class that implements the
IObjectSpaceProviderFactory
interface. Please refer to the CustomObjectSpaceProviderFactory.cs file for implementation details. This class is used as a service to createIObjectSpace
objects on demand. - Create the
ServiceCollection
to register required XAF services and yourCustomObjectSpaceProviderFactory
service. - Use the
IReportExportService
service to load and prepare a report.
Files to Review
Documentation
- How to: Use XAF Reports in a non-XAF Application
- Access XAF Application Data in a non-XAF Application
More Examples
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#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(); })
};
}
}
}
C#using Aqua.EnumerableExtensions;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.Core.Services;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.ReportsV2;
using DevExpress.Persistent.BaseImpl.EF;
using ExportReportDemo.Module.BusinessObjects;
using Microsoft.Extensions.DependencyInjection;
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
ServiceCollection serviceDescriptors = new ServiceCollection();
serviceDescriptors.AddXafCore();
serviceDescriptors.AddXafReportingCore();
serviceDescriptors.AddScoped<IObjectSpaceProviderFactory, CustomObjectSpaceProviderFactory>();
IServiceProvider serviceProvider = serviceDescriptors.BuildServiceProvider();
var typesInfo = serviceProvider.GetRequiredService<ITypesInfo>();
InitTypesInfo(typesInfo, serviceProvider.GetRequiredService<IObjectSpaceProviderFactory>());
RegisterBOTypes(typesInfo);
IReportExportService reportExportService = serviceProvider.GetRequiredService<IReportExportService>();
using var report = reportExportService.LoadReport<ReportDataV2>(data => data.DisplayName == "Employees Report");
reportExportService.SetupReport(report);
report.ExportToPdf("testEFCore.pdf");
Console.WriteLine("Done");
}
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));
}
}
}