Pages

Thursday, March 25, 2010

Getting started with Enterprise Library

As Software developers we experience common problems like Logging, Validation, Caching, Data access, Exception handling in our each and every software solutions. Implementing logging infrastructure or data access logic for every project is waste of time. Enterprise Library is set of best practices which recommended by Microsoft to be used in small to enterprise level applications.

Features come with Enterprise Library 4.1 (October 2008)

  • Caching Application block
  • Cryptography Application block
  • Data Access Application block
  • Exception Handling Application block
  • Logging Application block
  • Policy Injection Application block
  • Security Application block
  • Unity Application block
  • Validation Application block

Goals for Enterprise Library

Enterprise Library is a collection of application blocks and services intended for use by developers who build complex, enterprise-level applications. These applications are typically deployed widely and have interdependencies with other application and systems. In addition, they generally have strict security, reliability, and performance requirements.

The goals of Enterprise Library are the following:

  • Consistency. All Enterprise Library application blocks feature consistent design patterns and implementation approaches.
  • Extensibility. All application blocks include defined extensibility points that allow developers to customize the behavior of the application blocks by adding their own code.
  • Ease of use. Enterprise Library offers numerous usability improvements, including a graphical configuration tool, a simpler installation procedure, and clearer and more complete documentation and samples.
  • Integration. Enterprise Library application blocks are designed to work well together and are tested to make sure that they do. It is also possible to use the application blocks individually.

Getting started with Enterprise Library

Note: We need to install enterprise library only on Developer machines, no need to install it on deployment/production machines. Only required Enterprise library dlls should be copied to solutions bin folder or GAC.

  • Referencing Enterprise Library common files to Visual Studio project.

    Add a reference to the application block assembly (For demo purpose I am going to use Logging application block.)

    In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab, and then find the location of the application block assembly and select Microsoft.Practices.EnterpriseLibrary.Logging.dll and click ok.

    Use the same procedure to set a reference to the Enterprise Library Common assembly, Microsoft.Practices.EnterpriseLibrary.Common.dll and to the ObjectBuilder assembly, Microsoft.Practices.ObjectBuilder2.dll

    Now open the source code file (e.g Program.cs) and add following namespace on top of the code.

    using Microsoft.Practices.EnterpriseLibrary.Logging;

  • Adding Configuration entry to App.Config / Web.Config

    Right click on app.config or web.config file of your project and click on Edit Enterprice Library Configuration as sown bellow,

    Now right click on config file node from node tree and select New -> Logging Application Block as shown bellow,

    Now you can see default logging config settings are displaying in the node tree.

    Also it has added a Formatted
    Event Log Trace Listener to the Event Listeners section.

  • Now we are ready to go with Enterprise Library. Following is a simple example of writing Log entry to events log using Logging application block,


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.EnterpriseLibrary.Logging;


    namespace EntLibDemo
    {
    class Program
    {
    static void Main(string[] args)
    {
    LogEntry logEntry = new LogEntry();
    logEntry.EventId = 100;
    logEntry.Priority = 2;
    logEntry.Message = "Informational message";
    logEntry.Categories.Add("Trace");
    logEntry.Categories.Add("UI Events");


    Logger.Write(logEntry);
    }
    }
    }

See how easy to use Enterprise library and the Enterprise library configuration utility save us lots of time and now we can focus more on development of business features.

Download Source Code for this post.

Download PDF version of this post.

Resources

Download Enterprise Library 4.1 (Oct 2008)

Patterns & Practices – Enterprise Library latest on Codeplex

No comments: