December 21, 2014
Hot Topics:

Wire Up Data-Driven Web Apps with an ASP.NET 2.0 DataSource Control

  • June 7, 2006
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

ObjectDataSource Performance

The ObjectDataSource will create a new instance of the data component for each call that is performed, and it will call a public default constructor that must be present on the data component. This instantiation can have significant performance implications—especially if the data component does time-consuming work at the time it is instantiated. Some built-in mechanisms help accommodate this. For starters, you can use static rather than instance methods. As you probably know, static methods do not require you to create a class instance, which reduces the performance overhead.

If you do have cause to use an instance component, an ObjectCreated event allows you to custom initialize the component after it has been instantiated, and an ObjectCreating event allows you to respond before the object is created so you can customize how it is created. Similarly, an ObjectDisposing event fires just before disposing the data component. If the data component implements the IDisposable interface, the Dispose method will be called after the event.

You wire up the events by adding them to the ObjectDataSource declaration and registering the methods to call. A good use for the ObjectCreating and ObjectDisposing events would be to allow for an instance object to be stored in cache and to control the retrieval from cache to prevent it from being disposed.

Static Sample

Using a static method does not take any additional setup on the part of the ObjectDataSource. Rather than set the TypeName to an instance method, you simply point it to a static method. Here is a code snippet from the original example with the static keyword added to the method declaration:

using System;using System.Collections;using System.Data;using System.Data.SqlClient;using System.Configuration;public class Customers{   public Customers()   {   }   public static ICollection GetCustomers()   {      //Cut out sample ...   }}

ObjectCreated and ObjectDisposing Sample

The following code, an expanded sample from the earlier control declaration, demonstrates wiring up and handling the ObjectCreating and ObjectDisposing events:

<div>   <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"   SelectMethod="GetCustomers"   TypeName="Customers"></asp:ObjectDataSource></div><asp:GridView ID="GridView1" runat="server"              DataSourceID="ObjectDataSource1"              OnObjectCreating="ObjectDataSource1_ObjectCreating"              OnObjectDisposing="ObjectDataSource1_ObjectDisposing"></asp:GridView>

Here is sample code from the Web page code behind for handling the events:

//Method fired just after the instance is createdprotected void ObjectDataSource1_ObjectCreating(object sender,   ObjectDataSourceEventArgs e){   //Get or put something from cache through e.ObjectInstance = xxx}//Method fired just before disposing of the data componentprotected void ObjectDataSource1_ObjectDisposing(object sender,   ObjectDataSourceDisposingEventArgs e){   //Get or put something from cache and cancel event e.Cancel = true}

Here is sample code for the data component, as it must implement the IDisposable interface. This code is a snippet of the changes to make to the example above:

public class Customers : System.IDisposable{   public Customers()   {   }   void System.IDisposable.Dispose()   {      //...   }   //Cut out remaining sample ...}

You can see these events fire simply by putting a statement such as System.Diagnostics.Debug.WriteLine("xxx") in the method and using a break point to see it in the Visual Studio IDE.

What Have You Learned?

You've seen how to select data and display it in a GridView by binding to an ObjectDataSource. Because the insert and delete operations behave similarly to the update operation, they should be interchangeable with the examples. You also looked at the performance implications of using ObjectDataSource and some ways that you can ensure your applications perform well.

Future Columns

The topic of the next column is yet to be determined. If you have something in particular that you would like to see explained here, you could reach me at mstrawmyer@crowechizek.com

About the Author

Mark Strawmyer (MCSD, MCSE, MCDBA) is a senior architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design, and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the third year in a row. You can reach Mark at mstrawmyer@crowechizek.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel