This is a migrated thread and some comments may be shown as answers.

Dependency Injection with REST Service

1 Answer 52 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Aleksandar
Top achievements
Rank 1
Aleksandar asked on 25 Apr 2018, 07:28 PM

We have implemented a custom report resolver that returns an InstanceReportSource based on the report class name. The InstanceReportSource.ReportDocument is instantiated from our IoC container (Simple Injector) and all objects are injected into the report via a constructor as expected. Within each report we subscribe to the NeedDataSource event and retrieve the data from a repository instance that has been injected. However, when we attempt to retrieve data from the repository, the repository instance has already been disposed. The injected objects are scoped to the webapi request. I read in the forums that the processing of the report happens in a separate context. Could that be causing my issue?

We've used this approach for years with the WCF report service and it has worked well. How can we configure this to work with the REST report service? We are now using the WPF report viewer.

 

 

1 Answer, 1 is accepted

Sort by
0
Katia
Telerik team
answered on 01 May 2018, 01:47 PM
Hello Aleksandar,

Reports will need real objects, therefore, the dependency injection through a custom constructor will not be suitable. Consider the following approach for passing the repository object to the report.

custom resolver:
public class MyResolver : IReportResolver
    {
       public Telerik.Reporting.ReportSource Resolve(string reportStringDescription)
        {       
           Report reportInstance =null;
           if(reportStringDescription.Contains("SampleReport"))
           {    
               // create a repository object using the application context
               var repository = new GetRepository(new ApplicationDbContext());
               // here you can pass a real object to the report
               reportInstance=new Report1(repository);
           }
            return new InstanceReportSource { ReportDocument = reportInstance};
        }
    }

report:
public partial class Report1 : Telerik.Reporting.Report
    {
      public Report1()
        {
            InitializeComponent();
        }
       public Report1(repository)
        {
            InitializeComponent();
            this.DataSource = repository.GetData();
        }
    }


Regards,
Katia
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Aleksandar
Top achievements
Rank 1
Answers by
Katia
Telerik team
Share this question
or