When is Poor Man’s Dependency Injection appropriate?


When is Poor Man’s Dependency Injection appropriate?

Only in legacy code situations.

That’s it.  I was called out appropriately on forgetting this, but legacy code, where I have to apply dependency-breaking techniques, does not always afford the opportunity to apply Dependency Injection and IoC properly.

Perhaps another question would be – for what situation would it be appropriate to apply the Dependency Inversion Principle, but NOT DI and IoC?  For me, just one class implemented with PMDI is pain over using an IoC container.

If your application is so small that PMDI seems appropriate, then why bother at all?  Why not use RAD tools, go for a Linq2Sql backend, and slap it all together in a controller action?  If my app was so small that IoC is too much, there are LOT more optimizations I’d make towards that end.  If my app was so small that it won’t change enough to need DIP, then who cares?  I’m wasting the business’ money otherwise.

Also, I do not buy the legal argument.  If you can only use Microsoft products, then use the Microsoft container, Unity!

Classes should be very clear on what they do, and what they require to function.  If I see two constructors, one without arguments, this would imply the overloaded parameter-ful constructor contains optional dependencies, not required dependencies.

No Fluff Just Stuff roundup