©2006 Alex Homer – alex@stonebroom.com
This series of articles looks at the patterns & practices Enterprise Library, and how you can use it in your ASP.NET applications. The five articles are:
· Part 1 - An Introduction to Enterprise Library
· Part 2 - Working with Enterprise Library in ASP.NET
· Part 3 - Using Enterprise Library in ASP.NET Partial Trust Mode
· Part 4 - Extending Enterprise Library with Custom Providers
· Part 5 - Adding Configuration Design Support for Custom Providers
The patterns & practices (p&p) group at Microsoft has for some time been building reusable blocks of code that make it easier to add specific features to your applications. Termed Application Blocks, we have seen over the last few years the release individual blocks such as the Data Access Application Block, the Exception Handling Application Block, and the Logging Application Block. Predominantly aimed at Windows Forms (executable) applications, they demonstrate best practice coding techniques, provide a common platform for achieving what are often complex tasks, and help to simplify application development.
The major problem users encountered with the individual application blocks was choosing which to use, and then manually configuring them to provide the exact feature set required. You had to edit your application configuration file to add what was often a complex hierarchy of nested elements and attributes.
To solve these issues, the p&p group decided to combine all the blocks into a single deliverable called Enterprise Library. This includes a graphical configuration tool that you can use to add individual blocks to an application, and to edit the configuration afterwards as required. Now available in a version that runs within v 2.0 of the .NET Framework, Enterprise Library is proving a popular tool for many enterprise level and large scale application development scenarios.
Although not originally designed for use in ASP.NET, most features of Enterprise Library do in fact work well in this environment. In particular, developers are taking advantage of the Data Access Application Block, the Cryptography Application Block, the Exception Handling Application Block, and the Logging Application Block. The blocks that contain features supported directly by ASP.NET can also prove useful in specific scenarios. For example, unlike the ASP.NET Caching mechanism, the Caching Application Block allows the simultaneous use of both a sliding and an absolute expiry time for cached values. The Security Application Block also offers a more flexible approach for features such as authorization and security credential caching than ASP.NET if you need to roll your own mechanisms for this.
In addition, you can easily modify the behavior of the blocks by taking advantage of their pluggable architecture to replace the providers they use with your own custom provider classes. For example, as demonstrated in these articles, you can use a custom caching provider with the Caching Application Block to store the cached data in a format and location that exactly matches the requirements of your Web site, your network, and your application architecture.
Enterprise Library consists of a set of application blocks that solve common challenges. These blocks ship with full source code, allowing you to modify and extend them to exactly match your own requirements. There is also a useful set of helper classes within the blocks that are usable within applications built to almost any architectural style. Microsoft describes the library as "Architectural guidance embodied in code", demonstrating best practice in design and coding style and using the features of the .NET Framework to best effect. And, unlike many frameworks you might consider, Enterprise Library is completely free of charge!
The community site at http://practices.gotdotnet.com/projects/entlib contains links to download Enterprise Library and updates for the product, access the message boards and team blogs, and read the documentation. You can even see the plans for the next version of Enterprise Library!
Enterprise Library contains the following application blocks:
· The Caching Application Block, which provides a local cache for use in all applications
· The Cryptography Application Block, which provides hashing and symmetric encryption functions
· The Data Access Application Block, which provides standard database access functionality
· The Exception Handling Application Block, which provides a consistent strategy for processing exceptions
· The Logging Application Block, which provides standard logging functionality
· The Security Application Block, which provides authorization and security caching functionality
Enterprise Library also includes a set of core functions, including configuration, instrumentation, and object builder services, which all other application blocks take advantage of. Figure 1 shows the dependencies and the plug-in architecture for Enterprise Library. For example, the Caching Application Block uses features of the Cryptography Application Block to encrypt and decrypt cached data, while providing caching services to the Security Application Block. If you use a database to cache your data, the Caching Application Block uses features of the Data Access Application Block. This demonstrates how closely related and dependent the individual blocks are, and you can use individual features of the blocks within your own code in the same way.

Figure 1 - The dependencies and plug-in architecture of Enterprise Library
Enterprise Library also contains internal instrumentation as a core feature, which allows you to monitor performance of the code within the application blocks. The instrumentation includes Windows Events, Performance Counters, and WMI Events. To install and enable the instrumentation features, you run a utility that is provided with Enterprise Library from your Start menu, add the Instrumentation section to your App.config or Web.config file using the Configuration Console, and then edit the configuration values to "turn on" events and performance counters as required. This is the element that the Configuration Console adds to your configuration file, and you can edit the attributes manually if required:
<instrumentationConfiguration eventLoggingEnabled="true"
wmiEnabled="true"
performanceCountersEnabled="true" />
Enterprise Library uses pluggable providers to separate the code that carries of the individual block tasks from the provider that accesses the resources used by that block. For example:
· The Caching Application Block contains three built-in Backing Store Providers that allow you to store the cached data in isolated storage on the user's machine, in a database, or solely in memory.
· The Data Access Application Block contains Database Providers for access to common types of database system, including SQL Server, Oracle, or through an ODBC provider.
· The Logging Application Block contains individual Listeners, Filters and Formatters that control how and in what format logging data is exposed.
· The Cryptography Application Block contains several Hash Providers and Symmetric Encryption Providers that use different encryption algorithms.
You can also create your own custom providers by inheriting from a suitable base class (most application blocks provide one, such as the BaseBackingStore class in the Caching Application Block), or you can implement a pre-defined interface. Part 4 of this series of articles describes the process for creating a custom Backing Store Provider for the Caching Application Block.
Figure 2 shows a high-level view of the pluggable architectural approach within Enterprise Library. Your application references one or more application blocks, which in turn use individual providers to access their resources such as a database, the Event Log, or just in-memory data. At the same time, the blocks use features within the Enterprise Library core for tasks such as reading configuration data, creating object instances, and exposing instrumentation.

Figure 2 - The pluggable architecture approach and provider model within Enterprise Library
Installing Enterprise Library is easy. You must join the community at the GotDotNet Web site at http://practices.gotdotnet.com/projects/entlib, then download the .EXE file for Enterprise Library for .NET Framework 2.0 (January 2006 release) and run it. This installs and adds links to your Start menu for:
· The Library Application Blocks
· The Configuration Console
· Utilities to compile and install the Library
· Quick Start examples for each block
· VS 2005 projects for Library and Quick Starts
· Unit test projects for NUnit and VSTS
· Utilities to create and install databases, backing stores, and instrumentation
· Help files and release notes
You should also download and install any service packs or fixes for Enterprise Library. At the time of writing, the only patch available for the January 2006 release was the Partial Trust Patch (2554), which allows Enterprise Library to run in less than Full Trust mode. This is useful in ASP.NET applications, as you will see in Part 3 of this series of articles.
Figure 3 shows the Start menu structure created by the Enterprise Library installer. You can see the links to various batch files, the Configuration Console, Visual Studio 2005 solutions, the documentation, the online community, and Enterprise Library blogs. Immediately after installation, you should run the two batch files that build the library and copy the assemblies into the correct bin folder. You must also run these after you modify the contents of the source code for the library to ensure that the assemblies are up to date. By default, Enterprise Library installs into your Program Files folder using the path %Program Files%\Microsoft Enterprise Library January 2006. The correct location of the runtime assemblies is %Program Files%\Microsoft Enterprise Library January 2006\bin.

Figure 3 - The Start menu structure after installing Enterprise Library
To use the built-in instrumentation features of Enterprise Library, you must run the Install Instrumentation batch file before editing your application configuration file to turn on instrumentation. You can then use the Configuration Console to add the appropriate entries to your application configuration file and turn on the instrumentation features.
To view the entire source code for Enterprise Library, modify it, and run tests, use the links to the solutions on the menu shown in Figure 3. There are solutions configured to use the Visual Studio Team System test environment, the third party NUnit test utility, or a solution that is not configured for unit tests.
The Application Blocks for .NET section of the menu opens to provide links to each application block in Enterprise Library. For each block, there is a solution configured to use the Visual Studio Team System test environment, and one configured for the third party NUnit test utility. If the block requires additional installation tasks (as is the case with the Caching Application Block and the Logging Application Block), this section of the menu provides a link to a batch file that carries out this task for you.
To help you get started using the application blocks, Enterprise Library includes a series of Quick Start examples - simple Windows Forms applications that:
· Demonstrate the main features of the block
· Show how to use the block in your application
· Contain links to Walkthroughs that explain the workings of the block and the code
· Are provided in both VB.NET and C#
· Demonstrate best practice techniques
You can run each of the examples from your Start menu, though you should ensure that you run any other installation or setup batch files first, as described in the previous section. There is a Quick Start for each application block, plus two that help you set up a SQL Server-based configuration store for your application configuration data (see the following section for more details).
Figure 4 shows the Cryptography Application Block Quick Start. The Cryptography Application Block makes it easy to encrypt and decrypt data, create hashes of data, and compare hash values. Like all the Quick Start examples, Cryptography Application Block Quick Start demonstrates the basic features of the block, and you can view the code as you run the solution to learn more about how the block actually works. Notice also that it contains a button that opens the documentation to show the Walkthrough guidance for the example.

Figure 4 - the Cryptography Application Block Quick Start example
One of the biggest advances over the individual application blocks previously released as separate tools in the provision of a combined Configuration Console. This allows you to add individual blocks to your application, and set the behavior of each one, using a simple graphical user interface (as shown in Figure 5).

Figure 5 -The Enterprise Library Configuration Console showing configuration of the Caching Application Block
You can open an existing configuration file, or create a new one, and add just the blocks you need using simple right-click context sensitive menus. Figure 5 shows the configuration for an application that uses the Caching Application Block and the Data Access Application Block. You can see the settings for the Default Cache Manager, such as the expiration polling frequency, in the screenshot.
Another useful feature of Enterprise Library is that you are not limited to using XML-format configuration files to store your configuration data. Like all the application blocks, the Enterprise Library configuration system uses pluggable providers to read from and write to configuration files. Instead of the standard System Configuration Source provider that reads from App.config or Web.config, you can install other Configuration Source providers that read from XML-format configuration files with any name (the File Configuration Source), or install the SQL Configuration Source from the Start menu and store the configuration data in SQL Server.
· Go to Part 2 - Working with Enterprise Library in ASP.NET
©2006 Alex Homer – alex@stonebroom.com