Skip to main content

Dependency Injection for Dummies

In the OOP world, everyone is all the rage about this Dependency Injection stuff.  So what is it?  In order to explain DI, I have to use the term Inversion of Control, so perhaps we should start there?  Well, the truth is, the whole thing is purely about *drumroll please* Decoupling.

When decoupling code, you have to identify the links between pieces of code.  For example, when attaching an arm to a robot, you would have to plug it into a slot in the shoulder.  The decoupled part has to plug into the rest of the code somehow.  However, as with robots, this would get really complicated.  Each piece plugs in uniquely, so if we're plugging in a leg down at the bottom, but it's not working right, we have to check the connection there.

IoC is like having all of the plugs in the robot's head.  The parts might be shaped differently in order to plug into the head, but they're the same parts.  In programming, IoC is the concept of selecting the decoupled parts at the beginning of the code, rather than somewhere in the middle.  A more realistic programming example may help.  If we are writing code for a printer which has to print a picture, we're going to have code to select the picture, and define the size and layout of the paper.  Somewhere way down at the end, we'll have the code that actually starts the printer and prints the picture.  When we decouple our code, we remember that some day the user may want to switch printers, or maybe even use a 3d printer, so we'd decouple our printer code, or make it work on its own, like a robot's drawing hand (as that's essentially what it is).  In IoC, when we start up the program, the first thing we do is select which printer code we're using.  If you fail at IoC, you select your printer right before you use it, and you won't know until that point that the user plugged in a different printer today, one that you haven't written code to support yet.  The whole thing would throw an error much too late.

OK, so that's IoC.  What's Dependency Injection?  Dependency Injection is a pattern to use IoC in your code.  It's just a way of doing it.  Dependency Injection is based around using interfaces (or abstract classes) to create a contract for how the code will work. Then, your classes implement those contracts and, in theory, the other code in your project doesn't care how the classes implement the contract as long as they call the methods defined by the contract.

For clarity, we'll look at an example.  In our printer code, we might write an interface that defines a method called PrintImage(byte[] image); and then, we'd implement it with some printer-specific code about how to make that byte array into ink on the page.  All that matters to the calling code though is that it can call Printer.PrintImage(myImage); and not how the code implements it.

Now this is cool in concept, but it's pretty complex in practice, ok not too bad.  As a result of the complexity, there have been a lot of people out there who built a DI Framework of their own.  I personally have used Ninject and Unity, and, to be honest, as long as I can assign the interface a class just once when I start the program, and it populates all of my parameterized constructors, then I don't really care which framework I'm using.  Take a look at DI, I'm sure you'll find it easy to use, and great for decoupling code.


Popular posts from this blog

Teams and Complexity

Let's pretend you're a car mechanic (I don't know, maybe you are).  But you don't work at some shop in town, you work at a bigtime auto-maker onsite at the factory.  You help build cars from scratch.  You work with a lot of other car mechanics, and various engineers of different levels.  If we were looking at the 1910s, you might be working for Henry Ford on one of the first ever assembly lines.  Either way, you have a specific part of the car you're responsible for.  Your skills probably extend beyond that, and you might work on a different part of the car each day as your manager moves you around to keep the factory efficient.

One of your coworkers, Bob, is a superb engineer.  He is very good at building cars, far better than you, and he does it faster than everyone else.  Your boss really likes him.  You often get stuck after him in the assembly line, so you know exactly what sorts of things he does.  He's not sloppy, but he likes to do things his way.  He w…

Managing Programmers

Working with other programmers is tricky.  That said, it's nothing compared to the job of managing programmers.  One of my favorite quotes about Perl is that (paraphrased) "a Perl developer is like a rockstar.  Now imaging having a bunch of rockstars in one room together and you will understand why you don't want an entire team of Perl developers."  It's not about Perl here though. What's important to understand is that any developer worth his salt is going to be like a rockstar.  And yes, there are a lot of professional developers out there who aren't worth their salt, but that's for another post another day.  Rockstar may not be the right term here, but think of it this way.  These guys are smart.  They may not be geniuses, but there's going to be things that they know that you don't and probably never will.

I've seen it more than once and it's not going to make some Product Managers happy, but I'm going to state a fact, an eleph…

Managing Developers is HARD

I've been a software dev for a long time.  I've also been running my own software company for a few years now.  This is important information because of why I do these things.  I am a sofware developer because I love learning.  I slack off when doing a job that bores me, and software development always has something new to experience which keeps me excited and interested.  Why start a software company then?  That puts me in the role of manager rather than developer.  The truth is simple.  I've worked for a lot of companies, and I don't see any of them doing a great job of managing their software development.  That's not to say none of them have done a good job, but no one out there seems to be doing a great job.

How are they different?
A lot of companies get this part right.  Software developers are different from other employees.  The distinction is important in the same way it's important to acknowledge that an insurance agent is different from a construction…