Amazon.com Widgets Add Asynchronous Data Methods to the Enterprise Library

WilliaBlog.Net

I dream in code

About the author

Robert Williams is an internet application developer for the Salem Web Network.
E-mail me Send mail
Code Project Associate Logo
Go Daddy Deal of the Week: 30% off your order at GoDaddy.com! Offer expires 11/6/12

Recent comments

Archive

Authors

Tags

None

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.


    Add Asynchronous Data Methods to the Enterprise Library

    A Data Access Layer that does not support Asychronous 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 following download includes the modified binaries and my SqlDatabase.cs code file:

    AsyncEL.zip (348.39 kb)

    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:

     

        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 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 http://www.codeplex.com/entlib.

     

    kick it on DotNetKicks.com


    Posted by Williarob on Monday, December 01, 2008 8:50 AM
    Permalink | Comments (0) | Post RSSRSS comment feed
    blog comments powered by Disqus