Telerik Reporting R1 2017

Maintaining the lifecycle of the OpenAccessContext with the OpenAccessDataSource component

This section discusses how to maintain the lifecycle of the OpenAccessContext when using the OpenAccessDataSource component. The provided examples and code snippets assume an existing Telerik Data Access Model of the Adventure Works sample database with the following structure:

One possible approach to connect to a Telerik Data Access Model without using the OpenAccessDataSource component is to create a custom business object with a method that retrieves the necessary entities for the report. The sample code below defines a method that retrieves information about all products:

public class SampleBusinessObject
{
    public System.Collections.Generic.List<Product> GetProducts()
    {
        using (AdventureWorksEntities context = new AdventureWorksEntities())
        {
            return context.Products.ToList();
        }
    }
}

Then you can use ObjectDataSource to connect to that business object, as shown in the following code snippet:

var objectDataSource = new Telerik.Reporting.ObjectDataSource();

objectDataSource.DataSource = typeof(SampleBusinessObject);
objectDataSource.DataMember = "GetProducts";

var report = new Report1();

report.DataSource = objectDataSource;

The problem with this approach is that it is not guaranteed to work in all possible scenarios. Simple expressions accessing primitive properties of the Product entity might work as expected:

=Fields.Name

However, let us consider the following expression that is intended to obtain the category of a given product:

=Fields.ProductSubcategory.ProductCategory.Name

The above expression relies upon the lazy loading mechanism of Telerik Data Access to obtain the ProductSubcategory entity for the current Product entity via the corresponding relation property, and then the ProductCategory entity for the current ProductSubcategory entity. This is not guaranteed to work because the OpenAccessContext is already destroyed when evaluating the expression, so lazy loading is no longer possible. One of the greatest advantages of the OpenAccessDataSource component is that it can maintain the lifecycle of the OpenAccessContext automatically, so scenarios like the previous one are guaranteed to work as expected. When specifying a Type for the OpenAccessContext property, OpenAccessDataSource creates internally a new instance of the OpenAccessContext with the given type, keeps it alive for the duration of the report generation process, and then destroys it automatically when it is no longer needed by the reporting engine. The following code snippet accomplishes the previous task with the help of the OpenAccessDataSource component:

var openAccessDataSource = new Telerik.Reporting.OpenAccessDataSource();

openAccessDataSource.ObjectContext = typeof(AdventureWorksEntities);
openAccessDataSource.ObjectContextMember = "Products";

var report = new Report1();

report.DataSource = openAccessDataSource;

If you have already implemented your own mechanism for maintaining the lifecycle of the OpenAccessContext you can continue using the OpenAccessDataSource component. In this case however you need to specify a live instance of your OpenAccessContext to the ObjectContext property as demonstrated here:

var openAccessDataSource = new Telerik.Reporting.OpenAccessDataSource();
var openAccessContext = new AdventureWorksEntities();

openAccessDataSource.ObjectContext = openAccessContext;
openAccessDataSource.ObjectContextMember = "Products";

var report = new Report1();

report.DataSource = openAccessDataSource;

// You have to dispose the OpenAccessContext explicitly when done with the report.
openAccessContext.Dispose();