Inversion of Control is NOT about testability
Many times, I've heard that the best thing about IoC is that it enables testability. When dependencies are injected into a class, it's easy to test that class. Testability is a side-effect of good design. The lack of testability signifies a bad design, but the enable to test a class does not, by itself, signify a good design. The causality is one-way.
Let's take a look at an example:
public class MissileLauncher{ private INavigator _navigator; public MissileLauncher(){ _navigator = new MissileNavigator(); } public void Launch(){ //get direction guidance from _navigator and launch a missile } }
This can be a good design or bad design depending on your needs. We are coupling to an interface, INavigator, but we are also coupling to a specific implementation class, MissileNavigator.
If MissileLauncher can't do its job without MissileNavigator, then this coupling makes sense, and the class should be tested together with MissileNavigator. Other examples are operations on domain objects: The logic makes no sense without the Customer object, specifically, so the operation should be tested with the real Customer, and not a mock object.
If, however, MissileLauncher is a concept orthogonal to MissileNavigator, then INavigator adequately illustrates the necessary interaction. In this case, coupling to MissileNavigator is detrimental to our design if the MissileLauncher only depends on the operation exposed by INavigator.
We would change this class to the following:
public class MissileLauncher{ private INavigator _navigator; public MissileLauncher(INavigator navigator){ _navigator = navigator; } public void Launch(){ //get direction guidance from _navigator and launch a missile } }
Here, we have inverted the control over locating or creating the dependency. Now MissileLauncher does not care about the actual class, just that it receives an instance of the interface. With the interface it can do its job. This is a better design if you have determined MissileLauncher's behavior is orthogonal to the actual MissleNavigator class. Again, you have to make many hard decisions about design. Do you couple. . . or not.
In order to do anything useful in software, you must couple. You must couple to something.
The coupling determines how you will test. If you couple to a concrete class, you will test the code together with that class. If you only couple to an interface, you can test the class with only the interface.
Testability is a by-product of inversion of control, and design is the driving factor to invert the control or retain it. Don't invert all control just so each class can be tested independently of all other classes. You must decide where coupling is appropriate and where it is not.



Trackbacks
Inversion of Control happens to… at Rinat Abdullin Posted on 8.14.2008 at 5:46 PM
Pingback from Inversion of Control happens to… at Rinat Abdullin
Reflective Perspective - Chris Alcock » The Morning Brew #159 Posted on 8.15.2008 at 2:19 AM
Pingback from Reflective Perspective - Chris Alcock » The Morning Brew #159
Dew Drop - August 15, 2008 | Alvin Ashcraft's Morning Dew Posted on 8.15.2008 at 8:42 AM
Pingback from Dew Drop - August 15, 2008 | Alvin Ashcraft's Morning Dew
Weekly Web Nuggets #25 Posted on 8.15.2008 at 2:30 PM
General The Tortoise And The Hare : Tim Barcz has a must-read post comparing enterprise development to the fable of the tortoise and the hare. As someone who's been dealing with this sort of cycle for the last few years, I couldn't agree more. Diligent
Arjan`s World » LINKBLOG for August 15, 2008 Posted on 8.15.2008 at 3:13 PM
Pingback from Arjan`s World » LINKBLOG for August 15, 2008
Testability in .Net Posted on 8.15.2008 at 4:58 PM
Testability in .Net
Testability in .Net Posted on 8.15.2008 at 5:02 PM
Testability in .Net
Inversion of Control is NOT about testability Posted on 8.16.2008 at 10:41 AM
Inversion of Control is NOT about testability
The Path to Zen » Blog Archive » But Then There Is Also Unnecessary Coupling Posted on 8.18.2008 at 9:05 PM
Pingback from The Path to Zen » Blog Archive » But Then There Is Also Unnecessary Coupling
On good design and defining success Posted on 8.19.2008 at 8:27 PM
This is for the most part a reaction to conversations on design: Testability in .Net Design and Testability