August 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating an Application Development Framework Using Enterprise Templates - Part 1

  • May 23, 2003
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Data Access Application Block

When you are developing the data access layer for your application, most of the times, you will be writing the same data access plumbing code again and again. It would be very efficient to wrap the common data access code in the form of helper functions that allow you to call the stored procedure in a single line. With .NET, Microsoft themselves have created an application block named Data Access Application block that contains the optimized data access plumbing code that will help you call stored procedures and execute SQL statements against a SQL Server database.

We will use it as one of the building blocks as part of our distributed enterprise application template. This will go a long way in reducing the amount of custom code that we need to write, test and maintain. The data access application block also encapsulates performance and resource management best practices for accessing SQL Server database. Using the data access block, we can perform the following operations.

  • Invoke stored procedures or execute SQL commands
  • Specify parameter details
  • Return SqlDataReader, DataSet, or XmlReader objects

The data access block contains a class named SqlHelper that provides a set of static helper methods that can be used to execute a variety of different command types against a Sql Server database. It also contains a class named SqlHelpParameterCache that provides command parameter caching functionality that can be very useful in improving the performance of the application. The SqlHelper class provides five shared methods named ExecuteNonQuery, ExecuteDataset, ExecuteReader, ExecuteScalar, and ExecuteXmlReader. Each of the above methods provides a consistent set of overloads. This provides a well-defined pattern for executing a command by using the SqlHelper class.

For example, we can execute a stored procedure named GetCategories that takes a categoryID as an argument using the following lines of code.

DataSet ds = SqlHelper.ExecuteDataset( connectionString,           CommandType.StoredProcedure,           " GetCategories",           new SqlParameter("@iInformationTypeID", categoryID));

As you can see from the above, the ExecuteDataset method executes the stored procedure and returns a dataset as a result. For more information on data access application block, please consult the following link from Microsoft. Microsoft Application Blocks for .NET

For our application, we will download the data access application block from the above-mentioned link and add it to our enterprise template that we are going to be creating. Once the project is added to the template, it can easily be referenced from other projects in the solution using the Project References option. Once the reference is added, we can start using the SqlHelper class (that is part of the data access application block) methods to execute stored procedures and issue SQL commands. For the purposes of this article, we will create a custom data access project named SqlDataAccess that consists of the following classes.

  • SqlHelper class (that is part of the data access application block)
  • DBNullHelper class that provides helper functions for handling null values

You can download the code for the SqlDataAccess project along with the support material for this article.

Exception Management Application Block

In an ideal world, all the code you write would always run without error. But the reality is no matter, how carefully you write your code, errors can and will occur. For that reason, it is good to have an efficient error handling routine in place that can handle the errors in a graceful manner. It is also important to realize that the effectiveness of any exception handling solution is only gauged from the way it impacts the users experience when working with the application. Having seen the amount of time and effort spent by the developers in creating exception management systems, Microsoft created a powerful and extensible exception management application block that can be reused in any .NET application.

By using the exception management block, with a single line of application code you can easily log exception information to the event Log or extend it by creating your own components that log exception details to other data sources. As mentioned before, the Exception Management Application block can easily be used as a building block in any .NET application. The exception management block provides the following benefits.

  • Allows us to manage exceptions in our applications in an efficient and consistent way
  • Makes it possible to clearly isolate exception handling code from the application logic
  • Allows for handling and logging of exceptions with a minimal amount of custom code

To use exception management in our application, we need to perform the following steps.

  • Download the exception management application block from the following link. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/emab-rm.asp
  • Add the exception management and exception management interfaces projects to our solution.
  • Add reference to the exception management using Project References option.
  • Add using statement to reference the exception management block from your custom class.
  • Now you can publish exceptions by calling the Publish method of the ExceptionManager class as shown in the following line of code.
catch ( Exception ex ){   ExceptionManager.Publish( ex );}

Most of the times, you will be satisfied with the functionality of the default publisher, which writes exception details to the event log. However there are times, where you may want to extend the exception management block by writing your own custom publishers, which may log exception information to alternate locations. To accomplish this, you need to create a custom class that implements either the IExceptionPublisher or the IExceptionXmlPublisher interface defined within the Microsoft.ApplicationBlocks.ExceptionManagement.Interfaces.dll assembly.

For more information on the exception management block, please consult the Exception Management in .NET Architecture Guide.





Page 3 of 5



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel