Inverting Inversion of Control
What do you think about this code?
IoC.Container.Kernel.AddComponentInstance<IFoo>(this);
IBar context = IoC.Resolve<IBar>();//needs IFoo
For some reason it looks really funny to me.
More seriously, this is a good way to involve components that you cannot control in the container.
Comments
By "components that you cannot control" do you mean those provided by a third party for example? It seems like if you can reference it by "this" you probably have control over it... unless you are inheriting from something?
I ask because I had similar code at one point but factored it out as it seemed like I had the owner of the container at the wrong level.
By components that you don't control I mean stuff that is instantiated for you. Consider something like a COM component that is being instantiated for you by the runtime. You can probably work around that, but it is a real PITA to do, so this is a good way to handle that.
There are many cases where this is true, usually when you are writing plugins to things
This code makes a lot of sense to me.
E.g. imagine your application has command line arguments that influence what objects are created and with what parameters. This would be a perfect way to inject command line info into your IoC containter.
I asked question about this on the Castle forums in 2006:
http://forum.castleproject.org/viewtopic.php?t=1614
In that example I wanted to give the container an existing IPersonService instance and have the container set its Logger property with a Logger from the container.
Another use might be for dipping (think dipping an a cookie in a glass of milk: temporarly placing the instance in the container so its dependencies get satisfied then removing it) the returned handler from PageParser.GetCompiledPageInstance into the container so it can have additional properties filled before processing:
IHttpHandler handler =
PageParser.GetCompiledPageInstance(aspxPagePath, null, context);
// set the Logger property on the handler instance
if (handler is IRequiresLogger)
{
container.Inject<IRequiresLogger>(handler);
}
handler.ProcessRequest(context);
or in more general terms:
container.Inject(handler);
Comment preview