Building Dependable code with Dependency Injection – Slides & Source
Here are my slides and the source code to the TDD & DI talk I gave at CodeCamp Auckland 2008. My intent with the talk was to explain how TDD can be really good, and how DI can make TDD easier.
If you weren’t at my talk and have any questions – I’m in the market for more comments.
I’ve got one more link to add, these podcasts were what actually inspired my subject: http://channel9.msdn.com/shows/ARCast+with+Ron+Jacobs/ARCastnet-Presenter-First-Pattern-Part-1/
Here is my source code (perfectly commented), and here are my slides
Stay tuned for a bigger (50 line) do-it-yourself DI container that would be useful in production.
4 Comments for this post
Hey Rob,
Great presentation at code camp. Good job getting the slides up so quick.
Just one comment on your Q&A – there was a guy there who was asking you about DI breaking encapsulation. I happen to agree with him: Getting stuck having to expand some long lost project’s code that uses a lot of DI leaves you with a scavenger hunt to round up or reinvent all the right dependencies to feed the constructors. Fortunately, even if you haven’t completely embraced a DI framework, another easy answer is Factories. They can encapsulate the DI when you don’t want the consumers of your classes to have to be responsible for the dependencies.
Thanks Rob. I couldn’t make it to the code camp but I’ll take a look at this.
@Rob Engberg:
Hope it’s ok to post the resuls of our IRL conversation, i wanted to post my reply online in case anyone else is looking for the same answers:
If you use IOC (passing dependencies through the constructor), then testing becomes easy, but the production system looks a bit messy because everytime you need an X, you have to create a Y and a Z. This code gets duplicated everywhere you need to use one of your nicely tested objects. Yuk! You say it breaks the encapsulation OO rule, I say it breaks the “Single Responsibility Principle” and “Dont Repeat Yourself” too!
Yes: Factories are a great way to mostly solve these three problems. But I definately prefer using a Dependency Injection container. I have heard them referred to as “factories on steriods”. They are actually much easier to use, as well as being more powerful.
Hope you got a chance to read the article by James Kovacs (http://msdn.microsoft.com/en-us/magazine/cc337885.aspx) – it explains this stuff pretty well!
Cheers – Rob
@Rob & Rob,
if you feel DI/IOC for the testability is making you feel your code is hard to maintain, you could use a testability framework that eliminates the need for external dependencies.
With Typemock you can facilitate unit testing by eliminating dependencies by mocking, even if your dependency is on non-interface types, internal object creation etc.
This really isn’t supposed to be a sales pitch but you’re invited to visit our blog and website to learn more.
Leave a comment