I’ve been working on that IoC container I’ve blogged about. You know, the ultra-lightweight, delegate based at the root with reflection as optional, simple container. While doing so, I’ve also been learning about some of the other capabilities in other containers. One of the things I discovered was Guice and the "annotation" (that would be an attribute for us .NET developers) based configuration it uses. By itself, not all that exciting as I’d already expected to use attributes for specifying what to inject. But then I noticed that they allow you to annotate an interface. Wow. Now the following is working with my container.
ObjectContainer container = new ObjectContainer(); container.RegisterDynamicConstruction(); IFoo foo = container.Resolve<IFoo>();
No, nothing was left out there (other than the definition of IFoo and some other class that implements it). I’ve done no configuration. I just registered dynamic construction which allows the container to create types that haven’t been registered by using reflection. Then I instantiated an IFoo. Resolve looked for a registered factory for IFoo, and when it didn’t find one it looked at the attributes on IFoo to discover a "default" implementation type and instantiated that. This eliminates 90% of what I dislike about IoC containers. You don’t have to specify a long list of configurations (in code or in some config file, doesn’t really matter) to register types with the container even though 90% of the time you’ll want the same type. Why should I configure a container, just because I may want to mock a service out during testing?
Granted, I’m still specifying what types to create. However, it’s done in code, when defining the interface, and is applied any time I use that interface (unless overriden) in every application with out doing any further configuration. Nearly nirvana.
Unity (as well as other containers) may have this capability… I haven’t looked that close. It does allow you to instantiate concrete types through reflection, which will still do injection, so it wouldn’t surprise me too much if they went the extra few inches to this. If not, I believe it could be trivially added through an extension. So, the only major difference with what I’m building is the complexity/size. So there’s probably not a lot of interest in what I’m building to other folks. But I want it, and it’s been a fun project for discovery.