6

I am developing an asp.net mvc 5 web application. There is a class library only for telerik reports[no trdx]. I am using TypeReportSource to resolve report and NeedDataSource event of report file to fetch data from database. In the whole project i am using constructor injection(structuremap) technique but here Constructor injection is not working as telerik report only supports parameterless constructor.

How to pass data to DataSource of the report? I do not want to add separate IoC container for the class library as it is shared by multiple project with separate configuration.

Rased Dot Net
  • 530
  • 3
  • 14

2 Answers2

1

In "Telerik Report Library" mode, the every report are composed by 3 files by default. Let's say i create a report named "ProductReport" in report project. it will generate ProductReport.cs, ProductReport.Designer.cs, ProductReport.resx

Following is the "ProductReport.cs" code:

public partial class ProductReport : Telerik.Reporting.Report
{
    public ProductReport()
    {
        //
        // Required for telerik Reporting designer support
        //
        InitializeComponent();

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

I think one of the option is to add another constructor with parameter(s) which you want to inject in to Report instance, include the data source, and remember to call "InitializeComponent()".

Another good side of this solution is, it will not impact the report designer usage, and the modification of the report items by designer itself.

Ethan Wu
  • 428
  • 5
  • 15
0

Into the designer code of the report you can change the ObjectDataSource. It's an example with a List<> binded into the objectDataSource of an MVC test project. Here is my working code.

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();
Yannick Turbang
  • 378
  • 4
  • 8