Telerik blogs

The Q2 2010 release of Telerik Reporting introduces two new data source components that are aimed at improving the integration with business applications utilizing the ADO.NET Entity Framework or the OpenAccess ORM.
(You can see the new data source components in action in the free webinar tomorrow, Tuesday , Jul 20, 11 am EST. Just register for What’s New in Telerik Reporting and RadControls for WinForms. During the live event attendees will also have the chance to win a Telerik Ultimate Collection, valued at $1999).

Prior to the Q2 2010 Telerik Reporting release, the only option for connecting a report to an existing entity model was to create a class with a custom business logic responsible for all data retrieval from that entity model, and then use the ObjectDataSource component to data-bind the report to it. However that approach had an inconsistent design-time support at best, and also imposed several limitations at runtime, so it was evident a dedicated solution was necessary.

The Q2 2010 release introduces the new EntityDataSource and OpenAccessDataSource components that can be used to connect reports directly to an existing ADO.NET Entity Data Model or an Open Access Data Model respectively. The offered integration with the ADO.NET Entity Framework and the Open Access ORM is now completely seamless both at design-time and at runtime without the need of extra code. There are several benefits when using the EntityDataSource and OpenAccessDataSource components over the ObjectDataSource component:

  • Improved design-time support: this includes displaying the entity schema in the Data Explorer, Report Wizard, Table Wizard, and Crosstab Wizard; and a live data preview of the report in the Report Designer. The new data source components also have their own set of design-time editors and dedicated configuration wizards.
  • Improved runtime support: the new data source components can maintain automatically the entire life cycle of the object context at runtime. This relieves the developers from the responsibility to create and destroy that object context at the right times during the report generation process. Instead, the data source component can maintain its own instance of the object context and dispose it automatically when it is no longer needed by the reporting engine.

Each entity model can be treated as a special type of a business object layer generated by the ORM tool. In this regard the EntityDataSource and the OpenAccessDataSource components are similar to the ObjectDataSource component. For example, the following code snippet uses the EntityDataSource component to connect a report to an existing ADO.NET Entity Data Model:

C#:

var entityDataSource = new EntityDataSource();
 
entityDataSource.ObjectContext = typeof(MyContext);
entityDataSource.ObjectContextMember = "MyEntities";
 
var report = new Report();
 
report.DataSource = entityDataSource;

 
VB.NET:

Dim entityDataSource = New EntityDataSource()
 
entityDataSource.ObjectContext = GetType(MyContext)
entityDataSource.ObjectContextMember = "MyEntities"
 
Dim report = New Report()
 
report.DataSource = entityDataSource

 
Assign the type of the desired object context to the ObjectContext property of the data source, and the name of the property or method responsible for data retrieval to the ObjectContextMember property. At runtime, the data source component creates a new instance of the given object context, and invokes the specified member to obtain the entities needed by the report. The object context instance is kept alive for the duration of the report generation process and then destroyed automatically, when it is no longer needed by the reporting engine.

Here is a similar code snippet that uses the OpenAccessDataSource component to connect a report to an existing Open Access Data Model:

C#:

var openAccessDataSource = new OpenAccessDataSource();
 
openAccessDataSource.ObjectContext = typeof(MyContext);
openAccessDataSource.ObjectContextMember = "MyEntities";
 
var report = new Report();
 
report.DataSource = openAccessDataSource;
 

VB.NET:

Dim openAccessDataSource = New OpenAccessDataSource()
 
openAccessDataSource.ObjectContext = GetType(MyContext)
openAccessDataSource.ObjectContextMember = "MyEntities"
 
Dim report = New Report()
 
report.DataSource = openAccessDataSource
 

Both the EntityDatasource and the OpenAccessDataSource components offer a great deal of flexibility when connecting to an entity model. For example, let us extend the previous object context using a partial class that defines a method with custom business logic:

C#:

public partial class MyContext
{
    public List<MyEntity> GetMyEntities(string name, int value)
    {
        return this.MyEntities.Where(entity => entity.Name == name && entity.Value == value).ToList();
    }
}
 

VB.NET:

Partial Public Class MyContext
    Public Function GetMyEntities(ByVal name As String, ByVal value As Integer) As List(Of MyEntity)
        Return Me.MyEntities.Where(Function(entity) entity.Name = name And entity.Value = value).ToList()
    End Function
End Class
 

As usual, specify the type of the object context to the ObjectContext property, and the name of the method to the ObjectContextMember property. Additionally, if the method has any arguments, you can pass data source parameters automatically to them using the Parameters collection. The sample code below illustrates how to do this with the EntityDataSource component:

C#:

var entityDataSource = new EntityDataSource();
 
entityDataSource.ObjectContext = typeof(MyContext);
entityDataSource.ObjectContextMember = "GetMyEntities";
entityDataSource.Parameters.Add("name", typeof(string), "=Parameters.Name.Value");
entityDataSource.Parameters.Add("value", typeof(int), "=Parameters.Value.Value");
 
var report = new Report();
 
report.DataSource = entityDataSource;
report.ReportParameters.Add("Name", ReportParameterType.String, "MyName");
report.ReportParameters.Add("Value", ReportParameterType.Integer, 100);
 

VB.NET:

Dim entityDataSource = New EntityDataSource()
 
entityDataSource.ObjectContext = GetType(MyContext)
entityDataSource.ObjectContextMember = "GetMyEntities"
entityDataSource.Parameters.Add("name", GetType(String), "=Parameters.Name.Value")
entityDataSource.Parameters.Add("value", GetType(Integer), "=Parameters.Value.Value")
 
Dim report = New Report()
 
report.DataSource = entityDataSource
report.ReportParameters.Add("name", ReportParameterType.String, "MyName")
report.ReportParameters.Add("value", ReportParameterType.Integer, 100)
 

Here is the corresponding code snippet for the OpenAccessDataSource component:

C#:

var openAccessDataSource = new OpenAccessDataSource();
 
openAccessDataSource.ObjectContext = typeof(MyContext);
openAccessDataSource.ObjectContextMember = "GetMyEntities";
openAccessDataSource.Parameters.Add("name", typeof(string), "=Parameters.Name.Value");
openAccessDataSource.Parameters.Add("value", typeof(int), "=Parameters.Value.Value");
 
var report = new Report();
 
report.DataSource = openAccessDataSource;
report.ReportParameters.Add("Name", ReportParameterType.String, "MyName");
report.ReportParameters.Add("Value", ReportParameterType.Integer, 100);
 

VB.NET:

Dim openAccessDataSource = New OpenAccessDataSource()
 
openAccessDataSource.ObjectContext = GetType(MyContext)
openAccessDataSource.ObjectContextMember = "GetMyEntities"
openAccessDataSource.Parameters.Add("name", GetType(String), "=Parameters.Name.Value")
openAccessDataSource.Parameters.Add("value", GetType(Integer), "=Parameters.Value.Value")
 
Dim report = New Report()
 
report.DataSource = openAccessDataSource
report.ReportParameters.Add("name", ReportParameterType.String, "MyName")
report.ReportParameters.Add("value", ReportParameterType.Integer, 100)
 

If you need more information in this regard you can visit the following help topics from our online documentation:

If you are interested to see the other new Q2 features of Telerik Reporting, just register for the free webinar this Tuesday, Jul 20, 11 am EST and check out What’s New in Telerik Reporting and RadControls for WinForms. During the live event attendees will also have the chance to win a Telerik Ultimate Collection (valued at $1999).


About the Author

Stefan Tsokev

Stefan’s main interests outside the .NET domain include rock music, playing the guitar and swimming.

Comments

Comments are disabled in preview mode.