Share via


Dependency Injection with Unity Application Block

Today I had some time to have a look at Unity Application Block (https://www.codeplex.com/unity) 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 https://en.wikipedia.org/wiki/Aspect-oriented_programming

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