Big Data

Application development with Microsoft .NET application blocks

The .NET Framework brings object-oriented development to the Microsoft world. This is further enhanced by the Microsoft .NET application blocks. Learn how to enhance your development efforts with this new tool.


Microsoft offers application blocks for the .NET Framework. Application blocks encapsulate certain key areas by adding a layer over the .NET Framework, and they aid in faster application development and promote certain best practices. Here, I will discuss two examples of application blocks: the Data Access application block and the Exception Handling application block (see Figure A).

Figure A
.NET application blocks


Why use application blocks?
Application blocks encapsulate an optimized way of doing things in the .NET Framework into a reusable package, allowing you to cut down on redundant code. Consider an application requiring data access. Most of it is the same piece of code used repeatedly. The application block encapsulates this infrastructure code and provides a simple way to use the features that you may need in an optimized way.

Another advantage of application blocks is the fact that these plug-and-play components form a layer over the .NET Framework. The .NET Framework is continuously evolving—Microsoft has already released a version 1.1 beta, which makes quite a few changes to the class library. If you've used these application blocks, it will make it much easier to move to future versions of the .NET Framework. The application blocks wrap the framework, and even if the underlying framework introduces some changes, you can then make changes to just this wrapper.

By using application blocks, you'll retain the exposed methods of these building blocks and internally handle the changes in the .NET Framework. Your application is shielded from the changes that might occur in the .NET class library, thus enhancing maintainability. In fact, you can view these application blocks as black boxes through which functionality is used.

Data Access application block
The Data Access application block contains optimized code for data access. It encapsulates the System.Data.SqlClient namespace and may be used for accessing SQL Server data. You can download the Data Access application block here.

The Data Access application block is encapsulated in the Microsoft.ApplicationBlocks.Data namespace. First, you open the source code and build the assembly, adding a reference to the Microsoft.ApplicationBlocks.Data.Dll file. The Data Access block consists of two classes. A detailed view of the contents of the classes is shown in Figure B.

Figure B
Data Access block classes


The SqlHelper class has five static methods (shared in VB.NET) that are used for all common data access needs. Based on the situation and the return type needed, you can call the corresponding ExecuteXYZ( ) method of the SqlHelper class. For example, if you need a Dataset object returned, you can use the ExecuteDataset method.

Let's take a look at a VB.NET code snippet showing the SqlHelper class in use:
 
Dim dsProducts As DataSet
Dim sConnectionString As String = "Data Source=(local);Initial Catalog=Northwind;User Id=sa;Password="
dsProducts = SqlHelper.ExecuteDataset(sConnectionString, CommandType.Text, “SELECT * FROM Products”)



This returns a Dataset populated with the results from a query executed using the ExecuteDataset method. By making use of the CommandType parameter and setting it to CommandType.StoredProcedure, you can also call stored procedures. In this case, you can specify the stored procedure parameters.

You can make use of SqlHelper.ExecuteNonQuery whenever you make an INSERT, UPDATE, or DELETE statement. The ExecuteReader can be used when a stream of forward-only data is needed. The SqlHelper class also supports XML with the ExecuteXmlReader method, which can be used to retrieve data as XML. The ExecuteScalar method is used for retrieving a single value.

The SqlHelperParameterCache is a utility class that caches parameters used with the commands on SQL Server databases. The application blocks include complete source code in both VB.NET as well as C#.

Exception Handling application block
The Exception Handling application block contains optimized code for exception handling. You can download it here. It is encapsulated in the Microsoft.ApplicationBlocks.ExceptionManagement namespace. To use it, you must add a reference to the Microsoft.ApplicationBlocks.ExceptionManagement.dll.

The Exception Handling application block provides a way to manage exceptions in your applications. The main classes in this building block are the ExceptionManager, BaseApplicationException, and the ExceptionManagementSectionHandler class. A detailed view of the classes in this layer is shown in Figure C.

Figure C
Exception Handling block


Application exceptions are caught by the BaseApplicationException class. The ExceptionManager class is the key class. The ExceptionManager class has a Publish method used for handling the exception. This publishing mechanism is configured with an XML configuration file. Internally, the ExceptionManager class uses the ExceptionManagementSectionHandler class to retrieve the publisher settings from the XML configuration file. A default publisher logs the details of the Exception in the EventLog. You can also extend this building block by adding your own custom publishers. .

The only requirement is that these custom publishers should implement either the IExceptionPublisher or the IExceptionXmlPublisher interfaces. You can change the way the exceptions are published. This could be writing to a file, sending an e-mail notification, or logging the error in a database table. All you need to do is to encapsulate the algorithm to implement in a publisher—which implements one of the interfaces—and set the configuration file to use this publisher. The ExceptionManager uses the CustomPublisher for publishing exceptions.

This configuration XML file can be any of these three:
  • Machine.config (for all applications)
  • Web.Config (for Web applications)
  • ApplicationName.Exe.Config (for Windows applications)

Here is a sample XML fragment for configuring a custom publisher:
 
<configuration>
<configSections>
<section name="exceptionManagement"
type="Microsoft.ApplicationBlocks.ExceptionManagement
.ExceptionManagerSectionHandler,
Microsoft.ApplicationBlocks.ExceptionManagement"/>
</configSections>
<exceptionManagement>
<publisher mode="on" assembly="CustomPublisher"
type="CustomPublisher.ExceptionPublisher"
DB="Data Source=(local);User Id=sa; Password=;Initial Catalog=MyApp"  />
</exceptionManagement>
</configuration>

 

In case the CustomPublisher isn't available, the application block uses the default publisher. A VB.NET sample code is shown below, which illustrates how exceptions are published in code:
 
Try
objConn.ConnectionString = "Data Source=(local);
User Id=sa;Password=;Initital Catalog=SouthWind"
objConn.Open()
Catch objSqlEx As SqlClient.SqlException
ExceptionManager.Publish(objSqlEx)
Catch objEx As Exception
ExceptionManager.Publish(objEx)
Finally
objConn = Nothing
End Try

 

Any exception caught by the above code is published by the ExceptionManager.

Building a foundation
By using the Microsoft application blocks for .NET as the building blocks of your applications, you can hasten your development process and improve the maintainability of the application. The examples I've shared above can help you get off on the right foot when using these application blocks.

 

Editor's Picks