An Introduction and Overview of the Microsoft Application BlocksBy John Jakovich
If you're like me, you've probably been overwhelmed by the number of new data access objects in the .NET Framework. Maybe I'm lazy, but I've often thought to myself, ".NET is great, but I'm tired of creating four ADO.NET objects just to databind a control". You may have even coded methods (or classes) that "wrap" part of the data access process, in an attempt to reduce the amount of redundant code you have to write. Now, there's an easier way using Microsoft's freely available Application Blocks.
Application Blocks are C# and VB.NET classes distributed as Visual Studio projects that can be downloaded from Microsoft's Web site and used in any .NET application, including ASP.NET Web applications. They are useful and powerful tools that can make applications more maintainable, scaleable and efficient. In 2002, Microsoft released two Application Blocks that encapsulate two of the most common programming tasks: data access and exception management. These two Application Blocks contain classes, static methods and interfaces that greatly simplify SQL Server data access and application exception management.
In this article we will examine the two Application Blocks and discuss their benefits and how to get started using them! The article Examining the Data Access Application Block delves into the Data Access Application Block, examining how to use it in an ASP.NET Web application.
Introducing the Data Access and Exception Management Application Blocks
The Data Access Block provides static methods located in the
SqlHelperclass that encapsulate the most common data access tasks performed with Microsoft SQL server. If the term "static method" is new to you, it means that the class methods can be called without instantiating an instance of the class. For example, the method
SqlHelperclass can be called by simply using the statement
SqlHelper.ExecuteReader()-- no object instantiation of the
SqlHelperclass is required.
These static methods can be used to return
XmlReader objects containing database information. You can
call stored procedures and SQL commands and specify stored procedure parameter details. The
SqlHelper class also supports transactions.
To get started with the Data Access Application Block you must first download the installer at http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi. To learn more about this Application Block be sure to read the article Examining the Data Access Application Block, which examines this Application Block in more detail specifically examining the Block's various methods and how to utilize the Block in an ASP.NET Web application.
The second Application Block encapsulates exception management. The Exception Management block provides a framework enabling you to log exception information, isolate exception code from your business logic and manage exceptions efficiently and consistently. The Exception block also provides excellent base classes and interfaces that can be used to create your own exception management framework or extend the Application Block to met the needs of your particular system. The Exception Management Block can be downloaded at http://download.microsoft.com/download/VisualStudioNET/emabref/RTM/NT5/EN-US/ExceptionManagementApplicationBlock.msi.
Application Blocks were designed by reviewing successful and efficient .NET applications and combining the optimum implementations into classes for our use. As a result, Application Blocks incorporate the current best practices endorsed by Microsoft. However, the greatest facet of Application Blocks is that they are surprisingly well documented. Each Application Block is distributed with complete documentation and source code examples. Even the source code is commented. I highly recommend downloading the Blocks and browsing the code. Application Blocks are part of a larger effort by Microsoft to push their own variety of system architecture recommendations for .NET applications called Patterns and Practices.
A Brief Look at Patterns and Practices
Microsoft is strongly encouraging the concept of design patterns within .NET programming. In fact, they have an entire web site devoted to Patterns and Practices: http://msdn.microsoft.com/practices/. On their Web site Microsoft describes Patterns and Practices as:
"Patterns are an effective way to convey architecture, design and implementation guidance for .NET in a language that is already familiar to many enterprise IT professionals. Patterns are instantiated in Reference Architectures, Reference Building Blocks and IT Services, and LifeCycle Practices. Each patterns also has an associated GotDotNet Community for collaboration and discussion."
You may be asking yourself, "Sounds nice, but what exactly is a pattern"? A pattern is a way to describe commonly encountered problems. For example, many of us have written (or will write) code that authenticates users for access to a Web site. Our tools are frequently the same, username and password text boxes, a submit button and the code that is executed behind the scenes to perform the validation. The process of logging-in a user can be abstracted and defined as a pattern. We could further identify certain practices that would facilitate the authentication pattern, making it easier and more efficient to perform the various tasks involved in logging-in a user. The process of defining patterns enables us to design more abstract solutions to address the common situations we encounter when programming any system.
The key to finding the best solution for a pattern is not to ask yourself "How do I solve this problem" but instead, "How do I solve these types of problems". Application Blocks are Microsoft's implementation of two common patterns, efficient database interaction and consistent application exception management.
The Benefits of Using Application Blocks
Arguably, the biggest benefits to using Application Blocks are faster and more modular development. Because much of the "nuts and bolts" of data access and exception handling is wrapped in the Application Blocks, you don't have to worry about the tedious details required to return a DataReader or write an exception to the System Log. This allows us to concentrate on the business logic in our applications. Application blocks cut down on redundant code and provide a buffer between you and the .NET framework. Additionally, because Application Blocks are distributed as Visual Studio projects, you are free to browse the source code and make changes and/or additions to suit the needs of your own application. Finally, Application Blocks come with very good documentation and quickstart samples to get you up and running quickly.
Getting Started With Application Blocks
In order to begin using either of the Application Blocks you must first download and install them. As mentioned previously, the application blocks can be downloaded from the following locations:
- The Data Access Application Block - http://download.microsoft.com/download/VisualStudioNET/daabref/RTM/NT5/EN-US/DataAccessApplicationBlock.msi
- The Exception Management Block - http://download.microsoft.com/download/VisualStudioNET/emabref/RTM/NT5/EN-US/ExceptionManagementApplicationBlock.msi
The installer package will create an entry in your Start Menu -> Program Files called
"Microsoft Application Blocks for .NET". There you will find the source code (C# and VB),
documentation, and Quick Start examples. To begin, select either C# or VB from the "Microsoft
Application Blocks for .NET" menu The selected Project will open in Visual Studio. After the
project loads, take some time to browse the source code, then build the assembly (from the Build menu
select Build Solution). Once the assembly
has built, you can start using the Application Block(s) in your Visual Studio .NET projects by adding a
reference the created assembly. For example, to use the Data Access Application Block, you would
add a reference to the assembly
Microsoft.ApplicationBlocks.Data.dll. Finally, you will need
to add the appropriate
using (C#) or
Imports (VB) statements to reference
Microsoft Application Blocks are great tools that enable us to maintain consistency and efficiency in our applications. Using the Application Blocks makes our programs easier to write and maintain by encapsulating common tasks in easy to use classes and static methods. I think one of the best features of Application Blocks is the excellent documentation and samples provided. Using Application Blocks is a no-brainer!
For a closer look at the Data Access Application Block, be sure to read Examining the Data Access Application Block!