Unity Constructor Injection - Greediest or Most Parameters? The Line Has Blurred ... Maybe
by David Hayden, Florida .NET Developer
I have been on a whirlwind presentation tour. Tomorrow I will be giving my 4th presentation in less than a month at the IASA Tampa Meeting on Aspect-Oriented Programming. It has been fun, but I will enjoy the 3 week break before presenting again at the Orlando Code Camp.
During that break I want to spend a bit of free time doing a more in-depth look at Unity, which is the new dependency injection tool from Microsoft Patterns & Practices. I have done a few tutorials on Unity with regards to the ASP.NET MVC Framework and ASP.NET Model-View-Presenter, but they have just barely scratched the surface as to how Unity works.
One of the questions I asked early on about Unity is how it determines which constructor it will use / pick when choosing to create an object?
Now if you are familiar with ObjectBuilder, you will know that there is an InjectionConstructorAttribute that you can use to explicitly specify which constructor ObjectBuilder will use for object creation.
public ProductCatalog : IProductCatalog
{
public ProductCatalog() {}
[InjectionConstructor]
public ProductCatalog(IProductDescriptionService service)
{
//...
}
}
Unity uses ObjectBuilder2, which shares the same use of the InjectionConstructor Attribute for explicity specifying the constructor for injection / creation.
However, when you do not explicitly specify a constructor, which one does Unity use?
I asked the product team on day 1 as to whether Unity chooses the greediest constructor that it can satsify in these cases, and the answer was a YES.
However, at this point, it is kind of a blurry Yes, because Unity apparently just picks the constructor with the most parameters when there is no InjectionConstructor Attribute on the class. I just found this out today :)
Now, does Greediest Constructor = Constructor with Most Parameters? Well, yes and no.
Unity is a little different from say, Castle Windsor, because Castle Windsor will only create components that are in its container. Unity, on the other hand, will attempt to create any class whether it is in its container or not ( Interfaces are a different story ).
So when Castle Windsor goes to create a class, it chooses the greediest constructor it can satisfy based on the contents of its container. Unity is not limited to creating objects in its container, however, so the greediest constructor in its mind is the constructor with the most parameters. I mean, Unity thinks it can create them all :)
As you can imagine then, Unity doesn't have a sophisticated algorithm for deciding the greediest constructor, like Windsor. It apparently just goes right for the constructor with the most parameters :) And here is where the line between greediest constructor and constructor with the most parameters blurs in my opinion and could be considered the same or not the same depending on how you define these concepts.
I have no real point to this post other than I can see where the concepts of greediest constructor and constructor with most parameters can be blurred in these cases when the dependency injection tool is not limited to creating only those classes in its container. I expected an algorithm / discovery process like in the case of Castle Windsor, but Unity just goes for the constructor with the most parameters.
I, personally, think Unity will have to add a discovery process for finding the greediest constructor, because I think it realistically cannot just pick the constructor with the most parameters based on things out of the scope of this post. However, it is interesting to hear the differences in vocabulary and how things get blurred when the container is not limited to creating only those classes in its container. Interesting stuff.
Site: http://www.davidhayden.com/