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:
Then you can use ObjectDataSource
to connect to that business object, as shown in the following code snippet:
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:
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: