Thursday, September 23, 2004

Here is the first part of the Application Code Blocks.  In this part, we will give you a theoretical overview of the Data Access Application Block (DAAB) (Based of what I found for reading on the sites in the resources paragraph).  After this examination, we will add a second part, where we will change the DAAB for use with the Oracle DB.  After this, we will put a short overview of real life experience with our modified DAAB.

What is DAAB?

The Database Access Application Block is a .Net component that has optimized code on board to access your database.  Till version 2, this is only for a SQL database.  The component returns SqlDataReader, Dataset and XmlReader objects.

Download, what's included and user requirements?

You can download DAAB from the download center of Microsoft

After you download and installed the component, you will find a new menu under "start --> programs”: Microsoft Application Blocks for .NET.  Under this menu you will find:

  • The VS.Net project files
  • The documentation

Be sure that you have following requirements when running the DAAB version 2:

  • Win 2000 / Win XP / Win 2003
  • .Net Framework SDK 1.1
  • VS.net 2003
  • SQL Server 7.0 or later
Why using DAAB?

You have the traditional Data Access to applications (including web).  That Data Access can be from different sources:

  • Traditional RDBMS (MS SQL, Oracle, ...)
  • Custom Data Store (XML file, flat file, ...)

Using this, often leads to redundant, repeated code, which can be written by experienced developers in their sleep.  In this case the code repetition increase the change for a bug in the code.  And it cost time.  We can say that a typical Data Access can be split up in several parts:

  • Specify the query
  • Create connection object
  • Create command object
  • Add parameters if necessary
  • For a DataReader use ExecuteReader()
    For DataSets or DataTables use a DataAdapter and its Fill() method
  • Bind the control to the DataReader/DataSet/DataTable
  • Close DataReader, Connection

The solution is encapsulation or better-called wrapped classes.  Most of you will use a framework or has written their own wrapper.  Our wrapper could have step 1 till 5 included.
Another solution is to use the DAAB.

Graphical overview:


Basic procedure:
  • Download the application block from the MSDN site 
  • Navigate in your 'start menu --> program files' to "Microsoft Application Blocks for .Net", select "data access" and select the language of your choice.
  • The Application Block Project will open.  Build this project.
  • Once you got the assembly, do following in your project:
  • Add reference to "Microsoft.ApplicationBlocks.data.dll"
  • Add Using (C#) or Imports (VB.Net) for the "Microsoft.ApplicationBlocks.data.dll","System.Data" and "System.Data.SQLClient"

Be aware: If creating an ASP.Net project you has to do following steps:

  • Copy the DAAB Assembly to your web application's "/Bin" dir
  • In the ASP.net web page where you need DAAB add : <%@ Import Namespace=“Microsoft.ApplicationBlocks.Data” %>
Extensive Use of DAAB:

After installing and implementing the DAAB, you got the SqlHelper and SqlHelperParameterCache available in your application. 

The SQLHelper class provides us static methods, while SqlHelperParameterCache is used to improve performance. 

The SQLHelper class:

The SQLHelper provides us with 13 methods we can use (for a complete list, see the MSDN DAAB section - SqlHelper Class Implementation Details).  Each method has it's set of overloads.  This gives the developer all the flexibility how they want to access the data and how they want to pass the connection string, transaction and parameters.

We will show an example.

The Execute reader:

Normally you would write this kind of code :

SqlConnection myConnection = new SqlConnection(connString);

const string strSQL = "SELECT CustomerID, CustomerName FROM Customers ORDER BY CustomerName";
   
SqlCommand myCommand = new SqlCommand(strSQL, myConnection);

myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

When using DAAB, we could reduce the code to this:

const string strSQL = "SELECT CustomerID, CustomerName FROM Customers ORDER BY CustomerName";

SqlDataReader reader = SqlHelper.ExecuteReader(connString, CommandType.Text, strSQL);

Know that the every method has it's own set of overloaded method. For example: the ExecuteReader has also a method where you could pass an already open connection,...  Also you have other related methods, like:

  • Executing non-returning queries (Update/insert/...)
  • Executing scalar-returning queries
  • ...

These related methods, have also the overloaded options as the ExecuteReader().

Parameterize:

We can use parameters with the DAAB.  DAAB accepts two kinds of parameters:

  • Explicit sqlparameter Instances
      example:
        dgOrders.DataSource = SqlHelper.ExecuteDataset(connString, CommandType.Text, strSQL, new SqlParameter("@CustomerID", value));
  • Implicit parameter values
      example:
           dgProducts.DataSource = SqlHelper.ExecuteDataset(connString,"getOrders", value);

The explicit method can be used with SQL Query as with Stored Procedures.  The implicit method works only with Stored Procedures.

In the previous examples we only had one parameter.  What if you have more parameters? Well in this case you just include a comma-delimited list of parameter values:

  • dgProducts.DataSource = SqlHelper.ExecuteDataset(connString,    CommandType.Text, query, new  SqlParameter("@ParamName1", value1), new SqlParameter("@ParamName2", value2), …, new SqlParameter("@ParamNameN", valueN));
  • dgProducts.DataSource = SqlHelper.ExecuteDataset(connString, "getProducts", Value1, value2, …, valueN);

When using implicit parameters we have a performance issue.  DAAB has to do 2 steps:

  • Use the DeriveParameters method (SqlCommandBuilder class) to determine the parameters
  • Iterated through the parameter array to assign the values to the discovered parameter list.

To solve this problem, Microsoft has added the SqlHelperParameterCache class.

The SqlHelperParameterCache Class:

This class caches the SP parameter list.  The class contains 3 public methods to manage parameters.  These methods are :

  • Cache parameters: CacheParameterSet
  • Retrieve cached parameters: GetCachedParameters
  • Retrieve the correct parameters for a Stored Procedure: GetSpParameterSet

Internally, the Application block creates a hash table when storing your parameters.  The key of this hashtable is a simple string concatenating of the connectionstring and the command text.  When reading the parameters we will get the name, direction (In- of Output), the datatype and so on.

When using a Stored Procedure, we can use the 3rd method.  This method will connect to the DB, looking for the SP and retrieve all the information about the parameters for this SP.

Future :

Microsoft is working on the next version.  On the date of this writing it is version 3.  This version is available for test at the GotDotNet Community. This version provides the concept of an abstract provider, whereas version 1 & 2 using a concrete provider (SQLClient).  Version 3 will have 3 build-in providers : 

  • SQLServer
  • OleDb
  • ODBC

So it will be possible to use for instance an Oracle DB, or even (if you really, really wants MS Access).

See you at the next part.  In here we will talk about modifying the DAAB so it can be used with an Oracle DB.  In part 3, we will also describe our life experience of the DAAB.

Resources:

MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp
.Net Data Access Architecture Guide
4GuysFromRolla: http://aspnet.4guysfromrolla.com/articles/070203-1.aspx
GotDotNet: http://www.gotdotnet.com/workspaces/workspace.aspx?id=c20d12b0-af52-402b-9b7c-aaeb21d1f431
Microsoft Download :
http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi

9/23/2004 7:24:34 AM (Romance Standard Time, UTC+01:00)  #     | 
 Thursday, September 02, 2004

I hope this will be the start of a useful serie about Application Code Blocks.  It will be my first time experience with these code blocks, and I hope a good starter guide for other developers.

So what are application code blocks?

These are C# (and VB.Net) classes, which can be downloaded from the Microsoft website and used in any .Net application.  Even ASP.Net applications.  These powerful tools will make your applications more efficient and maintainable.  Those application blocks contain classes, methods and interfaces, which can be used.  A big advantage is that those application blocks are very well documented at the Microsoft site. 
A big benefit of using the application code blocks is faster and more modular development of your .Net application.  You don't have to worry about exceptions, ... because it's all in the application code block, so you can focus on your business layer.  Other advantages are:

  •  Buffer between you and the .Net framework
  •  Application Code blocks are distributed as VS.Net projects
  •  Make changes and/or additions that suits your needs

As a conclusion we can say that the application code blocks are great tools and they will add efficiency to our projects, because it will be easier to write our programs and to maintain them. 

My application code blocks pilot?

OK, I put the Application Blocks in a pilot project for myself.  I will use a few of those Blocks in the first place.  In total I have create 3 phases in whom I will describe a few of those blocks :

  • Phase 1: Till October 1st 2004
  • Phase 2: Till December 31st 2004
  • Phase 3: Beginning of 2005

Code blocks that are available at this moment and which I'm going to look at:

  • Aggregation Application Block
  • Asynchronous Invocation Application Block
  • Authorization and Profile Application Block (Phase 1)
  • Caching Application Block
  • Configuration Management Application Block  (Phase 3)
  • Data Access Application Block (Phase 1 and 2)
  • Exception Management Application Block (Phase 2)
  • Enterprise Notification Reference Architecture for Exchange 2000 Server
  • Logging Application Block (Phase 3)
  • Microsoft Content Integration Pack for Content Management Server 2001 and SharePoint Portal Server 2001
  • Smart Client Offline Application Block
  • Updater Application Block
  • User Interface Process Application Block (Phase 3)

More info can be found at the patterns & practices: Code MSDN page

Also, check the Coming Soon Section (Patterns & practices: Coming Soon):

  • Enterprise Library
  • Enterprise Development Reference Architecture - VS 2005 “Whidbey” version

Also Available : Beta release of the Testing .Net Application Blocks Guide.  Very interresting for reading when you have integrated or customized an Application Block in your own application.

Stay tuned for the part in this series, where I will examine the code blocks as we are going to use them.

Resources:

MSDN: http://www.microsoft.com/resources/practices/default.mspx
4GuysFromRolla: http://aspnet.4guysfromrolla.com/articles/062503-1.aspx
GotDotNet : http://workspaces.gotdotnet.com/pagtest

9/2/2004 8:25:28 AM (Romance Standard Time, UTC+01:00)  #     |