Feature Post

Top

Microsoft Data Access Application Block

Microsoft Enterprise Library: Microsoft Application Blocks (Data Access)

I have been using Microsoft’s Data Access Application Block for windows forms; and this time I plan to use ‘em in my ASP.NET project. I tend to like application block framework, mostly because for me it saves a lot of time and it provides a bug free code.

I mean seldom you feel a need to write your own data layer unless otherwise is explicitly required/demanded. So I get more time to think about my applications’ logic.

Just for those who have no idea what a data access application block is, following would reveal the before-and-after scenarios; and how it can push you into the “flow” that we seek during our development phase.

1. Code - Without application block
Following the code that create, and opens a connection, make a query, load the data into result and then binds it to the grid.

// Open connection
SqlConnection theConnection = new SqlConnection("Server=(local);Database=TestDB;Integrated Security=True;");
theConnection.Open();

//Create stored procedure command object
SqlCommand theCommand = new SqlCommand("GetEmployeesByDepartment", theConnection);
theCommand.CommandType = CommandType.StoredProcedure;

//Add parameter for stored procedure
theCommand.Parameters.Add("@DepartmentID", SqlDbType.Int); 
theCommand.Parameters["@DepartmentID"].Value = 9; 

//create DataAdapter and DataSet objects
SqlDataAdapter theAdapter = new SqlDataAdapter(theCommand);
DataSet dsResult = new DataSet("Result");

//fill dataset
theAdapter.Fill(dsResult);

//databind the grid
DataGrid1.DataSource = dsResult;
DataGrid1.DataBind();

//clean up
theConnection.Close();

2. Code - With application block
Following does the same as above but with lesser code.

//Make connection
string theConnection = "Server=(local);Database=TestDB;Integrated Security=True;";

//Execute
DataSet dsResult = SqlHelper.ExecuteDataset(theConnection, CommandType.StoredProcedure,"GetEmployeesByDepartment", new SqlParameter("@DepartmentID", 9) );

//Assign and bind
dgvResult.DataSource = dsResult;

dgvResult.DataBind();

Data Access Application Block
The Data Access Application Block simplifies many common data access tasks such as reading data for display, passing data through application layers, and submitting changed data back to the database system. It includes support for both stored procedures and in-line SQL, and provides access to the most often used features of ADO.NET in simple-to-use classes.

Key Scenarios
The Data Access Application Block is suitable if you encounter any of the following
situations:
  • Using a DataReader or DataSet to retrieve multiple rows of data.
  • Executing a command and retrieve the output parameters or a single-value item.
  • Performing multiple operations within a transaction.
  • Retrieving XML data from a SQL Server.
  • Updating a database with data contained in a DataSet object.
  • Adding or extend implementations of database providers.
When to Use
The Data Access Application Block is ideal for addressing the following requirements:
  • You need simplicity and convenience while helping developers use the functionality provided by ADO.NET with best practices.
  • You need to reduce the requirement for boilerplate code to perform standard data access tasks.
  • You need to maintain consistent data access practices, both within an application and across the enterprise.
  • You need to make it easy to change the target database type through configuration, and reduce the amount of code that developers must write when they port applications to different types of databases.
  • You need to relieve developers from learning different programming models for different types of databases.
Considerations
The following considerations apply to using the Data Access Application Block:
  • The Data Access Application Block is a complement to ADO.NET; it is not a replacement.
  • If your application must retrieve data in a specialized way, or take advantage of features specific to a particular database, consider using ADO.NET directly.

Download; you may download the msi from here.

Reference to assembly; add reference to the Microsoft.ApplicationBlocks.Data.dll assembly and start using Microsoft.ApplicationBlocks.Data.

MSDN; Also go through the Data Access Application Block documentation at Microsofts website.

Btw, v5.0 is still(as of now); under construction.

Microsoft Application Architecture Guide - Second Edition 2009 (p489)