I watched the battle from the sidelines where Jimmy Bogard talked about how not to do “poor man’s dependency injection”. Then Tim Barcz gave his input. Then Chad Myers got into the mix. Considering this blog is called Inversion of Control Freak, I should probably add my $.02 now that things have settled down.
I’ve gone back and forth on this issue where I really want to allow flexible wire-up of my components using the IoC container of choice, but at the same time, I’d like to have the ability to just skip the wire-up and have a default, ready-to-go implementation. In other words, I want all the power and flexibility and all the convenience at the same time. I want my cake and I want to eat it too!
Rinat Abdullin, co-creator of the coolest IoC container Autofac, had a good idea that I had kicked around in his most recent blog post. Previous to reading his post, I had considered just creating a simple, default factory class that would do the default wire-ups and go from there. Rinat, in his article, said the following (about 2/3 the way down the page) regarding the creation of a factory method:
By the way, that’s a good approach for creating developer-friendly libraries. We implement all functionality as separated and decoupled components (as in component-driven development) ready to be consumed by whatever IoC Container the developer likes. Extensibility and flexibility come in there natively.
Should the project be too simple for wring up the proper IoC infrastructure, developer-friendly library will have some static factory [method] to use. This class would serve as a logical entry-point into the library and would host hard-coded creation of components (using recommended configurations and settings).
There’s the answer. Just create a simple factory method. Those that want to swap default implementations can do so using constructor-based injection, while those that just want to use the library without all of the fuss can do so as well.