IUnityContainer generic methods moved to extensions... why ?

Feb 26, 2010 at 9:16 AM

In 1.x the generic methods were on the IUnityContainer interface but with version 2.0 they are now an extension methods.  This means that where ever IUnityContainer is exposed used you now have to add a using statement for Microsoft.Practices.Unity in order to use the generic method.

This wasn't necessary before and in my opinion just pollutes the using statements at the top. 

What was the justification for moving all the generic methods off the base interface.

To give some context we use a static class to store all container definitions.  This code now requires a using statement in the code file for the Foo class in order to use the generic version of resolve.

public static class Unity
{
	private static Dictionary<string, IUnityContainer> containers = new Dictionary<string, IUnityContainer>( StringComparer.OrdinalIgnoreCase );

	public static IUnityContainer Container( string name )
	{
		return containers[ name ];
	}
	/* ... Initialisation code removed for brevity */
}

public class Foo
{
	public ISomeInterface Bar()
	{
		return Unity.Container("Name").Resolve<ISomeInterface>();
	}
}
Feb 27, 2010 at 3:55 AM

The primary reason we made the change was because IUnityContainer had what, 44 methods on it? And grew some more in V2. That's just too many methods for one type. The methods in question were all just brain-dead convenience overloads. Brain-dead in the sense of trivial to implement, not brain-dead as in too stupid to exist or anything. Anyway, now that I'm going to be talked to by Microsoft HR, let's finish the discussion...

In the previous version, we didn't use extension methods for this because we were targetting VS2005, and it didn't have extension methods. Then we had to introduce the UnityContainerBase class instead just to carry around the extra overloads. And then we ended up having to carry around the overload on both the interface and the base class. It was just getting confusing, and was going to get worse over time. Using the extension method approach gives us the same basic programming experience (with the exception that, in some cases where you didn't need it you now do need that using statement) with a much simpler organization in the actual container project.

It's kind of funny - when Unity 1.0 was released, I had a bunch of people yelling at me because the interface was so big and we should have used extension methods. Now I'm getting yelled at because the interface doesn't have the overloads and I did use extension methods. I just can't win. ;-)

 

Feb 28, 2010 at 8:05 PM

thanks for the honest reply..... maybe a compromise here.

BuildUp and Registration method overloads as extensions are you need to add the using statement anyway

Resolve and ResolveAll on the interface ( basically anything that returns T or Something of T )

 

Cheers...

Robert