Cook Computing

Dependency Injection

February 5, 2009 Written by Charles Cook

I've become a big fan of Dependency Injection over the last few months. I was adding new functionality to a large .NET application which had not been designed with unit testing in mind and I found it difficult to create unit tests for the new components I was writing. I had to use a ServiceLocator type mechanism which could be configured during unit testing to supply mock types. Getting this into place required a substantial amount of work on top of the new functionality I was implementing. I am now using the Unity DI container on another project and finding that DI really does make a difference, separating the provision of services to classes from the functional structure of the application.

During this time I came across the Google Testing Blog. I found a lot of useful posts on here, particularly those by Miško Hevery, which helped clarify why the legacy code I was working with was so difficult to test.

Via Mike Hadlow, I discovered a couple of days ago that Dhanji R. Prasanna, another engineer at Google, is working on a book called Dependency Injection which looks interesting. A draft of the first chapter is available free online and is worth reading if you want an introduction to Dependency Injection.