Resovling policy enabled objects with Unity 2.0

· June 14, 2010

I had the opportunity to use Unity and Enterprise Libarary in my current project. On of the really cool features of Enterprise Library (and most Dependency Injection frameworks) is the support for Aspect Oriented Programming. It’s a really neat way of handling the cross-cutting concerns in your application.

I was therefore very surprised when I had a really hard time to get, what I thought was simple, the following scenario to work:

I want the objects that I resolve to be “policy enabled” – i.e. configured in such a way that I can add policies in the configuration that can be picked up later and applied to the resolved objects.

That is, it was hard in Enterprise Library 4.1 and Unity 1.2. I had to scan the net and put together a solution of my own. It was a mix of extension methods, wrappers and some low-level enterprise library tweaking. You know, the kind of code that doesn’t make you feel proud after you wrote it.

And then Unity 2.0 and Enterprise Library was released. All my problems gone! Everything is simple and the sun is always shining. … Not quite – the scenario I was looking for was a bit tricky still. But the solution a lot cleaner.

I asked a question about in on both StackOverflow and in the Unity Community and soon got an answer. In the unity thread I’ve posted a complete code example of the solution.

In short the answer was to add an extension called EnterpriseLibraryCoreExtension, like this:

_container.AddNewExtension<EnterpriseLibraryCoreExtension>();

Using this extension will instruct Unity to pick up my configuration with, in this example, the policies and apply them to the resolved object.

Twitter, Facebook