具有依赖注入的Telerik Reporting ObjectDataSource

6
我正在开发一个asp.net mvc 5网站应用程序。有一个类库只用于telerik报表[没有trdx格式]。我使用TypeReportSource来解决报告,并使用报告文件的NeedDataSource事件从数据库获取数据。在整个项目中,我都使用构造函数注入(structuremap)技术,但这里构造函数注入不起作用,因为telerik报表仅支持无参构造函数。
如何将数据传递给报告的数据源?我不想为类库添加单独的IoC容器,因为它被多个具有不同配置的项目共享。
2个回答

1
在“Telerik Report Library”模式下,默认情况下,每个报告由3个文件组成。 假设我在报告项目中创建了一个名为“ProductReport”的报告。它将生成ProductReport.cs、ProductReport.Designer.cs和ProductReport.resx三个文件。
以下是“ProductReport.cs”代码:
public partial class ProductReport : Telerik.Reporting.Report
{
    public ProductReport()
    {
        //
        // Required for telerik Reporting designer support
        //
        InitializeComponent();

        //
        // TODO: Add any constructor code after InitializeComponent call
        //
    }
}

我认为其中一种选择是添加另一个构造函数,其中包含您想要注入到报告实例中的参数(包括数据源),并记得调用“InitializeComponent()”。

这个解决方案的另一个好处是,它不会影响报告设计器的使用,也不会影响设计师本身对报告项的修改。


0

您可以在报表的设计代码中更改ObjectDataSource。这是一个示例,将List<>绑定到MVC测试项目的objectDataSource中。以下是我的工作代码。

using System.Collections.Generic;
using WebApplication1.Models;

namespace WebApplication1
{
    partial class Report1
    {
        #region Component Designer generated code
        /// <summary>
        /// Required method for telerik Reporting designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

            this.objectDataSource1 = new Telerik.Reporting.ObjectDataSource();
            List<Class1> list = new List<Class1>();
            list.Add(new Class1 { Nom = "Turbang", Prenom = "Yannick", Pays = "Belgium", Ville = "Arlon" });
            list.Add(new Class1 { Nom = "Turbang2", Prenom = "Yannick2", Pays = "Belgium", Ville = "Arlon2" });

            this.objectDataSource1 = new Telerik.Reporting.ObjectDataSource();
            this.objectDataSource1.DataSource = list;
            this.objectDataSource1.DataMember = "Class1";

            //Generated code
            //.....
            //.....
            Telerik.Reporting.Group group1 = new Telerik.Reporting.Group();
            Telerik.Reporting.Drawing.StyleRule styleRule1 = new Telerik.Reporting.Drawing.StyleRule();
            Telerik.Reporting.Drawing.StyleRule styleRule2 = new Telerik.Reporting.Drawing.StyleRule();
            Telerik.Reporting.Drawing.StyleRule styleRule3 = new Telerik.Reporting.Drawing.StyleRule();
            Telerik.Reporting.Drawing.StyleRule styleRule4 = new Telerik.Reporting.Drawing.StyleRule();
            Telerik.Reporting.Drawing.StyleRule styleRule5 = new Telerik.Reporting.Drawing.StyleRule();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接