The other day I was watching a video about IoC/DI from Google and about 47 seconds into the video I got a bit upset. Why did I get a bit upset? Because off the bat he talks about IoC/DI as being a concept to simplify testing and creating testable code. I am sorry but this is NOT the goal of IoC/DI, but rather a nice to side affect but OMG, it should NEVER be the goal.
In my opinion DI/IoC concepts are designed to give you loosely coupled code, code that has very limited knowledge of its dependencies and code which can be refactored/changed easily.
Now do not get me wrong, if you do follow DI (or any of the SOLID principles for that fact) you WILL indeed have code which is more testable than if you did not follow these principles. But to me the fact your code is more testable is a nice side affect of having good, clean code.
So, just to recap….
When you follow DI (or SOLID) principles you are creating code which should become simpler to maintain. This in turn should provide you code which is simpler to tests. It is NOT the other way around.
Till next time,
05-22-2009 6:24 AM