Click here to Skip to main content
15,890,845 members
Articles / Programming Languages / C#

Add Asynchronous Data Methods to the Enterprise Library

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 Mar 2009CPOL1 min read 14.6K   5  
Add asynchronous data methods to the Enterprise library

A Data Access Layer that does not support Asynchronous I/O is not scalable. Period. However, since Microsoft kindly provided the source code along with the Enterprise Library, it is possible to make the Enterprise Library's Data Application Block scalable by adding the Asynchronous methods (BeginExecuteNonQuery, BeginExecuteReader, etc.) to the SQL Database Object. I took the code from the 3.x library and modified the file SqlDatabase.cs found here c:\EntLib3Src\App Blocks\Src\Data\Sql (assuming you installed the source files to the root of your C drive). The download file above includes the modified binaries and my SqlDatabase.cs code file:

I don't think I implemented every overload, and again this is for the 3.0 library, but you should be able to modify the 4.x libraries and add more overloads by following the patterns I'm using. To use the binaries included in the zip file above, simply drop them into the bin folder of your web site or web application project, add references if necessary and don't forget to add the async=true attribute to both your <% page %> tag and your connection string! Here is a simple example of how you might use it, to get you started:

C#
 1 using System;
 2 using System.Collections;
 3 using System.Configuration;
 4 using System.Data;
 5 using System.Data.SqlClient;
 6 using System.Linq;
 7 using System.Web;
 8 using System.Web.Configuration;
 9 using System.Web.Security;
10 using System.Web.UI;
11 using System.Web.UI.HtmlControls;
12 using System.Web.UI.WebControls;
13 using System.Web.UI.WebControls.WebParts;
14 using System.Xml.Linq;
15
16 public partial class temp : System.Web.UI.Page
17 {
18     protected void Page_Load(object sender, EventArgs e)
19     {
20         if (!IsPostBack)
21         {
22             RegisterAsyncTask(new PageAsyncTask(new BeginEventHandler(
                   BeginUpdateByAsyncEL), new EndEventHandler(EndUpdateByAsyncEL),
                   new EndEventHandler(AsyncUpdateTimeout), null, true));
23         }
24     }
25
26     /// <summary>
27     /// Creates a SqlDatabase Object and stores it in HttpContext for the
       /// duration of the request.
28     /// </summary>
29     /// <value></value>
30     /// <returns>A SqlDatabase Object</returns>
31     /// <remarks></remarks>
32     public static Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase AsyncNorthWindDB
33     {
34         get
35         {
36             if (HttpContext.Current == null)
37             {
38                 return new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(
                       WebConfigurationManager.ConnectionStrings[
                       "AsyncNorthwind"].ConnectionString);
39             }
40             if (HttpContext.Current.Items["AsyncNorthWindDB"] == null)
41             {
42                 HttpContext.Current.Items.Add("AsyncNorthWindDB",
                      new Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase(
                      WebConfigurationManager.ConnectionStrings[
                      "AsyncNorthwind"].ConnectionString));
43             }
44             return HttpContext.Current.Items["AsyncNorthWindDB"];
45         }
46     }
47
48     /// <summary>
49     /// Begins an asynchronous call the Northwind Database
50     /// </summary>
51     /// <param name="sender"></param>
52     /// <param name="e"></param>
53     /// <param name="cb"></param>
54     /// <param name="state"></param>
55     /// <returns>An IAsyncResult Interface</returns>
56     /// <remarks>These methods do not exist in the standard
       ///          Enterprise Library</remarks>
57     public IAsyncResult BeginUpdateByAsyncEL(object sender, EventArgs e,
           AsyncCallback cb, object state)
58     {
59         SqlCommand cmd = new SqlCommand(
               "Update Products SET UnitPrice = 79.0 WHERE productID = 20");
60         return AsyncNorthWindDB.BeginExecuteNonQuery(cmd, cb, state);
61     }
62
63     public void EndUpdateByAsyncEL(IAsyncResult ar)
64     {
65         AsyncNorthWindDB.EndExecuteNonQuery(ar);
66     }
67
68     /// <summary>
69     /// Async Timeout method
70     /// </summary>
71     /// <param name="ar"></param>
72     /// <remarks></remarks>
73     public void AsyncUpdateTimeout(IAsyncResult ar)
74     {
75         Label1.Text = "Connection Timeout";
76     }
77 }

Download the full Enterprise library (including the complete source code) from here.

kick it on DotNetKicks.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Salem Web Network
United States United States
Robert Williams has been programming web sites since 1996 and employed as .NET developer since its release in 2002.

Comments and Discussions

 
-- There are no messages in this forum --