Dependency Injection with Unity Application Block

Today I had some time to have a look at Unity Application Block ( and I’ll share my first impression about it.

What Unity does is simply help you manage cross cutting concerns in your application. For those of you who are familiar with AOP (Aspect Oriented Development) you already know the story. If you have no idea what AOP is you can start with

Instead of creating dependency to the subject cross cutting class (for example Logger) Unity lets you create a dependency on interfaces (say ILogger). Later during runtime you can decide which logger to use (Logger, MyLogger, SomeOtherLogger etc)

You may ask “what about decoupling, we couple to the interface”, but I think the idea in Unity is not to create a totally decoupled system but go after loose coupling as much as possible. In the previous example you could end up coupling with all the loggers (Logger, MyLogger etc) instead you couple with the interface (ILogger) only.

And what about the complexity using Unity introduces? Is it worth coupling your application with Unity to loose couple your cross cutting concerns :) I think the answer depends on your cross cutting classes. If you’re sure that you’ll always depend on a single type (only to Logger but not MyLogger, SomeOtherLogger) you may prefer not to use Unity, otherwise Unity can help a lot.  

What if you had a totally transparent AOP framework that you could inject anything without doing zero changes to your classes? I have some thoughts (actually concerns) about it as well and I’ll be writing on the subject later..

Comments (2)

  1. sidarok says:

    Hi Hakan, great blog, great post thanks! Unity App block is great really, but I wonder should it be dead short after MEF is released. That’s one of the biggest fears for enterprise software vendors, and I presume on Microsoft side there is a strong fuss about what way of dependency injection will be adopted in the future, since there are hell a lot of things going on in that area in MS Sidar (Prism, Unity, Policy Injection App block, Did I mention that Acropolis was suspended ? omg :))

    Spring.NET has an AOP framework and the AOP in spring for JAVA  ( receives lots of good feedbacks from community, it might be worthwhile to look at really! (I believe Castle also has a solution for that but am really not familiar with that.

    The next step seems to be Dependency Injection Hell ! :)

    Thanks for the post, iyi sanslar.