011-04-04 How to: Display Non-Persistent Objects in a Report(如何:在报告中显示非持久性对象)

How to: Display Non-Persistent Objects in a Report(如何:在报告中显示非持久性对象)

This topic describes how to create a report based on non-persistent data. Use this approach to analyze and report data obtained from runtime calculations, stored procedures, arbitrary SQL queries, or third-party services.
本主题介绍如何基于非持久性数据创建报告。使用此方法分析和报告从运行时计算、存储过程、任意SQL查询或第三方服务中获得的数据。

Create a Report and Bind It to a Non-Persistent Data Type(创建报告并将其绑定到非持久性数据类型)

1.Declare a non-persistent class (for example, MyNonPersistentObject), and decorate it with the DomainComponent and VisibleInReports attributes.
声明一个非持久性类(例如MyNonPersistentObject),并使用DomainComponent和VisibleInReports属性对其进行装饰。

C#
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
// ...
[DomainComponent, VisibleInReports]
public class MyNonPersistentObject : NonPersistentBaseObject {
    public string Name { get; set; }
}

2.Create a report for the MyNonPersistentObject data type. You can add a predefined static report in Visual Studio or create a report at runtime.
为MyNonPersistentObject数据类型创建报表。可以在Visual Studio中添加预定义的静态报表,也可以在运行时创建报表。
在这里插入图片描述

Important
Non-persistent objects do not support the ViewDataSource component – use CollectionDataSource to bind a report to data.
非持久性对象不支持ViewDataSource组件-使用CollectionDataSource将报表绑定到数据。

At this step, the created report displays no data in the preview.
在此步骤中,创建的报表在预览中不显示任何数据。
在这里插入图片描述

Supply the Report with Data (Initialize Non-Persistent Objects)(为报告提供数据(初始化非持久性对象))

In .NET Applications(在.NET应用程序)

In .NET applications, you can use one of the following techniques to generate report data.
在.NET应用程序,您可以使用以下技术之一来生成报表数据。

Use the Application Builder(使用应用程序生成器)

In the Application Builder code, handle the NonPersistentObjectSpace.ObjectsGetting event as shown below:
在Application Builder代码中,处理NonPersistentObjectSpace. ObjectsGet事件,如下所示:

File: MySolution.Blazor.Server\Startup.cs, MySolution.Win\Startup.cs

C# (Blazor) 
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
// ...
// Handle the `NonPersistentObjectSpace.ObjectsGetting` event.
builder.ObjectSpaceProviders.Events.OnObjectSpaceCreated = context => {
    if (context.ObjectSpace is NonPersistentObjectSpace nonPersistentObjectSpace) {
        nonPersistentObjectSpace.ObjectsGetting += NonPersistentObjectSpace_ObjectsGetting;
    }
};
// ...
private static void NonPersistentObjectSpace_ObjectsGetting(object sender, ObjectsGettingEventArgs e) {
    // In the event handler, populate the `e.Objects` collection
    // with non-persistent objects of the required type.
    if (e.ObjectType == typeof(MyNonPersistentObject)) {
        BindingList<MyNonPersistentObject> objects = new BindingList<MyNonPersistentObject>();
        for (int i = 1; i < 10; i++) {
            objects.Add(new MyNonPersistentObject() { Name = string.Format("Object {0}", i) });
        }
        e.Objects = objects;
    }
}
// ...
Implement an IObjectSpaceCustomizer(实施IObjectSpaceCustomizer)

1.In the module project, implement the IObjectSpaceCustomizer service as shown below:
在模块项目中,实现如下所示的IObjectSpaceCustomizer服务:

File: MySolution.Module\NonPersistentObjectSpaceCustomizer.cs

C# 
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;


public class NonPersistentObjectSpaceCustomizer : IObjectSpaceCustomizer {
    private readonly IObjectSpaceProviderService objectSpaceProvider;
    private readonly IObjectSpaceCustomizerService objectSpaceCustomizerService;


    public NonPersistentObjectSpaceCustomizer(
    IObjectSpaceProviderService objectSpaceProvider,
    IObjectSpaceCustomizerService objectSpaceCustomizerService) {
        this.objectSpaceProvider = objectSpaceProvider;
        this.objectSpaceCustomizerService = objectSpaceCustomizerService;
    }


    public void OnObjectSpaceCreated(IObjectSpace objectSpace) {
        if(objectSpace is NonPersistentObjectSpace nonPersistentObjectSpace) {
            // Handle the `NonPersistentObjectSpace.ObjectsGetting` event.
            nonPersistentObjectSpace.ObjectsGetting += NonPersistentObjectSpace_ObjectsGetting;
            nonPersistentObjectSpace.ObjectByKeyGetting += NonPersistentObjectSpace_ObjectByKeyGetting;
            nonPersistentObjectSpace.Committing += NonPersistentObjectSpace_Committing;
            nonPersistentObjectSpace.PopulateAdditionalObjectSpaces(objectSpaceProvider, objectSpaceCustomizerService);
        }
    }


    private void NonPersistentObjectSpace_ObjectsGetting(object? sender, ObjectsGettingEventArgs e) {
        // In the event handler, populate the `e.Objects` collection
        // with non-persistent objects of the required type.
        if (e.ObjectType == typeof(MyNonPersistentObject)) {
            BindingList<MyNonPersistentObject> objects = new BindingList<MyNonPersistentObject>();
            for (int i = 1; i < 10; i++) {
                objects.Add(new MyNonPersistentObject() { Name = string.Format("Object {0}", i) });
            }
            e.Objects = objects;
        }
    }
    private void NonPersistentObjectSpace_ObjectByKeyGetting(object? sender, ObjectByKeyGettingEventArgs e) {
        //...
    }
    private void NonPersistentObjectSpace_Committing(object? sender, CancelEventArgs e) {
        //...
    }
}

2.Register your IObjectSpaceCustomizer service implementation in the Starup.cs file of the Blazor and WinForms applications. Use the TryAddEnumerable method to register these services:
在Blazor和WinForms应用程序的Starup. cs文件中注册您的IObjectSpaceCustomizer服务实现。使用TryAddEnumable方法注册这些服务:

File: MySolution.Blazor.Server\Startup.cs, MySolution.Win\Startup.cs

C# (Blazor)
public void ConfigureServices(IServiceCollection services) {
    //...
    services.TryAddEnumerable(ServiceDescriptor.Scoped<IObjectSpaceCustomizer,
        NonPersistentObjectSpaceCustomizer>());
    //...
}
C# (WinForms)
public static WinApplication BuildApplication(string connectionString) {
    var builder = WinApplication.CreateBuilder();
    //...
    builder.Services.TryAddEnumerable(ServiceDescriptor.Scoped<IObjectSpaceCustomizer,
        NonPersistentObjectSpaceCustomizer>());
    //...
}

The following image demonstrates the result:
下图演示了结果:
在这里插入图片描述

In .NET Framework Applications(在.NET框架应用程序)

1.Open the WinApplication.cs and/or WebApplication.cs code. Ensure that the NonPersistentObjectSpaceProvider is registered in the overridden CreateDefaultObjectSpaceProvider method (in addition to the existing XPObjectSpaceProvider or EFObjectSpaceProvider). The Solution Wizard automatically adds this code but it might be missing if you used an older XAF version to create your project.
打开WinApplication. cs和/或WebApplication.cs代码。确保NonPersistentObjectSpaceProvider已在覆盖的CreateDefaultObjectSpaceProvider方法中注册(除了现有的XPObjectSpaceProvider或EFObjectSpaceProvider)。解决方案向导会自动添加此代码,但如果您使用较旧的XAF版本创建项目,则可能会丢失此代码。

File: MySolution.Win\WinApplication.cs, MySolution.Web\WebApplication.cs

C#
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
    // ...
    args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
}

2.Subscribe to the XafApplication.ObjectSpaceCreated event, and subscribe to the NonPersistentObjectSpace.ObjectsGetting event in its handler. In the ObjectsGetting handler, check if the requested object type is a MyNonPersistentObject type and populate the e.Objects collection.
订阅XafApplication. ObjectSpaceCreated事件,并在其处理程序中订阅NonPersistentObjectSpace.ObjectsGet事件。在ObjectsGet处理程序中,检查请求的对象类型是否为MyNonPersistentObject类型并填充e.Objects集合。

File: MySolution.Module\Module.cs

C#
using System;
using System.ComponentModel;
using DevExpress.ExpressApp;
// ...
public sealed partial class MySolutionModule : ModuleBase {
    //...
    public override void Setup(XafApplication application) {
        base.Setup(application);
        application.SetupComplete += Application_SetupComplete;
    }
    private void Application_SetupComplete(object sender, EventArgs e) {
        Application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
    }
    private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
        var nonPersistentObjectSpace = e.ObjectSpace as NonPersistentObjectSpace;
        if(nonPersistentObjectSpace != null) {
            nonPersistentObjectSpace.ObjectsGetting += ObjectSpace_ObjectsGetting;
        }
    }
    private void ObjectSpace_ObjectsGetting(Object sender, ObjectsGettingEventArgs e) {
        if (e.ObjectType == typeof(MyNonPersistentObject)) {
            BindingList<MyNonPersistentObject> objects = new BindingList<MyNonPersistentObject>();
            for (int i = 1; i < 10; i++) {
                objects.Add(new MyNonPersistentObject() { Name = string.Format("Object {0}", i) });
            }
            e.Objects = objects;
        }
    }
}

The following image demonstrates the result.
下图演示了结果。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值