A Dependency Injection Analogy
There are many articles already discussing the details of various dependency injection implementations in a C# context. There's no point me writing another one. What I want to add is an analogy that helps you understand the benefits of DI. It's not always clear how DI adds to the efficiency of what you're coding, hopefully this will help a bit by showing you what you would end up prior to introducing DI.
Let’s start with the following class:
- public class CordlessDrill
- {
- public void Drill()
- {
- var drillBit = new DrillBit();
- drillBit.Turn();
- }
- }
But now we’ve come across a different task that needs doing; we need to saw wood. Well the class we’ve just wrote is useless to us, so we create another class – another tool to do this particular job:
- public class CordlessSaw
- {
- public void Saw()
- {
- var sawBlade = new SawBlade();
- sawBlade.Saw();
- }
- }
We’ve got enough tools to do both the jobs so far, but as we tackle new tasks and their requirements, we’ll need new classes. It’s inefficient to keep writing these ‘cordless’ classes (i.e. carry around as many tools as there are tasks), when in fact they share some common functionality. And that’s when it’s time for dependency injection.
If we disregard the actual details (the ‘implementation’) of the power tools dependency (i.e. whether it’s a saw or a drill bit), we can focus on the fact that it needs an attachment of some sort and that it will provide power to that attachment – that it is the only job. At this point, what does being this vague allow us to do? It allows us to define the attachment just as an attachment; an abstraction, rather than a concrete implementation. We could translate this, in real terms, to “I know the tool needs an attachment, I don’t care whether it’s a drill bit or saw blade”. Our code can then start to look like this:
- public interface IAttachment
- {
- void DoTask();
- }
- public class DrillBit: IAttachment
- {
- void DoTask()
- {
- // drill stuff
- }
- }
- public class SawBlade: IAttachment
- {
- void DoTask()
- {
- // saw stuff
- }
- }
- public class CordlessTool
- {
- private IAttachment _attachment;
- public CordlessTool(IAttachment attachment)
- {
- _attachment = attachment;
- }
- public void GivePowerToAttachment()
- {
- _attachment.DoTask();
- }
- }
There are a few things that haven’t been covered here, as they have been well covered in the other articles I mentioned earlier. For example, which IAttachment is injected into the CordlessTool class is a decision that does need making at some point. Typically, this is done with an inversion of control container. Also, we’ve opted for constructor injection here – as it mostly suits the requirements of this example. There are other ways of injecting dependencies.

Amit ChoudharyPosted Oct 25, 2015, 2:37 PM
Thanks Santhakumar Munuswamy!!
Santhakumar MunuswamyPosted Oct 25, 2015, 11:40 AM
Good one
Sibeesh VenuPosted Oct 23, 2015, 1:36 AM
Nice Share
Priyaranjan K SPosted Oct 22, 2015, 12:25 PM
Nice Share
Francis SusaimichaelPosted Oct 22, 2015, 7:37 AM
Thanks for the explanation!
Nilesh JadavPosted Oct 22, 2015, 2:50 AM
Good Work sir !!