Geeks With Blogs

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites


Anthony Trudeau

The other day Lindsay Rutter with Microsoft did a webcast on the Enterprise Library.  I've used the exception handling block in a few projects, but my opinion had been that the library was bloated and was low on my priority list of technologies to investigate.

The talk on dependency injection in the webcast changed my mind.  I immediately got a return on my investment as I wanted a more flexible method for tracing then what is offered from the Trace class in the System.Diagnostics namespace.  And most importantly I didn't want any unnecessary dependencies in my library.  Basically, my library wants the ability to log, but doesn't want any responsibility for it.  Here's what I did:

  1. Added a reference to the Microsoft.Practices.Unity library.  You'll need the Microsoft.Practices.ObjectBuilder2 library referenced wherever you create the unity container and inject the dependency.  My library doesn't care about that.  All it cares about is that whatever creates the key objects creates the dependencies.
  2. Define the interface for classes to perform the work.  I created the ILoggingHandler with methods such as LogError, LogWarning, and LogInformation.
  3. Add a using statement for Microsoft.Practices.Unity to the code files containing your classes that you will be injecting.
  4. Add the following code to the classes that I want to log.  There are undoubtedly other ways to approach this, but this is the most simple.  At first I didn't think it was very elegant to add a property to my class, but these are base classes providing a service to subclasses, so I warmed up to the idea.

    [Dependency]

    public ILoggingHandler LogHandler { get; set; }


  5.  Use the property within your code where necessary as in the following code.

    LogHandler.LogInformation("Enter trace information here");

 It's perfect for what I needed and is extremely simple to implement.  When it comes to creating the logging service class I can create it and inject it from the code that hosts these classes using the following code (where DefaultLoggingHandler is my implementation of ILoggingHandler).

IUnityContainer container = new UnityContainer();

container.RegisterType<ILoggingHandler, DefaultLoggingHandler>(new ContainerControlledLifetimeManager());

MySubClass obj = container.Resolve<MySubClass>();


The optional parameter I specified in the call to RegisterType allows the object to be created as a Singleton.  That means that every MySubClass object I create will use the same DefaultLoggingHandler.

I suggest that anyone who has discounted the Enterprise Library in the past take another look.  I first used the Enterprise Library in one of it's first interations.  It's grown and matured and can make some common, time consuming development activities go away.

Posted on Friday, June 26, 2009 12:49 PM .NET | Back to top


Comments on this post: Enterprise Library

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Anthony Trudeau | Powered by: GeeksWithBlogs.net