The Unity Inversion of Control Container

Feb 15, 2008 at 1:23 AM
Edited Feb 15, 2008 at 2:37 PM
P&P Team,

I've been a big fan of Object Builder since it was first released, so I'm very excited to see your team finally deliver a container. I've only perused the code for a few minutes, but so far I generally like what I'm seeing. I really, really like the direction you took with Extensions. This was an excellent way to cleanly expose the strategy/policy functionality in Object Builder.

The only thing I dislike so far is your choice to explicitly provide methods for designating types as singletons. I'm sure this was chosen because it was believed that the creation of singletons would be a frequent need, but this is only one strategy for how you might choose to manage the lifetime of an object and it seems as though you could have provided this ability using a more consistent approach. Again, I've only looked at the code for a few minutes and may not have completely understood the API yet, but couldn't this have been done using a SingletonExtension and the TConfigurator Configure<TConfigurator>() method like so?

container.Register<ILogger, TraceLogger>()

Certainly this is not quite as concise as the use of the SetSingleton<T> method, but it has the advantage of presenting a consistent approach, reducing the IUnityContainer API, and demonstrating how other such strategies might be accomplished. To me, this is similar to having the following be declared in the IUnityContainer:

IUnityContainer SetSlidingCachedInstance<T>(TimeSpan span);

IUnityContainer SetSlidingCachedInstance<T>(string name, TimeSpan span);

IUnityContainer SetSlidingCachedInstance(Type type, TimeSpan span);

IUnityContainer SetSlidingCachedInstance(Type type, TimeSpan span, string name);

The only other issue I have is with the description. Unity appears to be an Inversion of Control container, not merely a dependency injection container. For example, using Unity to register events as in your QuickStart would technically be an example of Inversion of Control, not of Dependency Injection. The name Dependency Injection wouldn't have existed if Martin Fowler didn't like generalities, but I believe mislabeling is worse than being too general. Please consider changing the description to be: Unity: a lightweight extensible inversion of control container.

That said, I like the overall approach and commend the P&P team on this delivery. Thanks.


Feb 16, 2008 at 1:22 AM
Derek, thanks for the kind words. I'm pretty happy with the extension API myself, but I designed it so my bias disqualifies me.

We've had two people comment about SetSingleton being too special case, and two other people who've commented that they want to be able to register and set singleton all at the same time in the Register call. Since the two groups are equally annoyed, it looks like we've done our job. ;-)

Singleton, transient, and pre-baked instances are three things I've needed repeatedly, so they're the bare minimum needed for lifetime management. Beyond that, I'd like to leave lifetime management in the extensions for the time being and see what evolves.

Feb 16, 2008 at 2:23 AM
Just so you know, I've prototyped an extension on Lifetime management that I'll show you next week at the workshop if you feel like it!
Feb 16, 2008 at 2:30 AM
Edited Feb 16, 2008 at 3:33 AM

I'm not sure these two requests are at odds so much as they represent different perspectives. One is asking why you decided to go with separate hot and cold nozzles with your new bath tub design. The other is asking why you have your new bath tub hooked up in your living room. If the people asking to set and register singletons at the same time were to take a few steps back, they too might conclude that the SetSingleton() doesn't really fit in the IUnityContainer interface.

While there doesn't appear to be a lot of traffic here yet, I would be interested in seeing what a larger sampling of the users think concerning this matter.

Feb 16, 2008 at 3:51 PM

Jimmy also started a similar discussion here: where I had a chance to comment.