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.

Comments

Popular posts from this blog

When Is Software Done?

I have some very exciting news.  A piece of software I've been working on for over 2 years is released to the general public!  This is a little exciting if it were software I'd been working on for some big company.  It's very exciting because it's software I have been working on for my company.  That's right!  My company is ready to start selling software and start making money!

I'm not gonna use this blog post to talk about my company and what it does.  You can read about that in our press release.  Instead, I'm going to talk about the software industry and the concept of done.  Because, as with everything, it's more complicated than it seems.

Software is never really done
Actually that's a misnomer.  Software can really be done.  But done is sort of a quantum state--there and not there at the same time.  First and foremost, anyone can understand that software that works is complete.  If the software's purpose is to process a credit card, if th…

How to identify a skilled programmer during an interview

How does one identify a skilled programmer?  No company that has interviewed me could tell the difference between myself and other programmers they'd interview.  The interview process is truly a game of luck in this industry--on both sides.  Both the programmer and the company are basing their actions entirely on luck.

Companies have come up with numerous methods to attempt to discern a good programmer from a bad one.  The best tricks they have include a series of math problems, algorithms, problem solving technique tests, and even obscure programming questions, some without definitive answers.  As an example: Is there an authoritative source of information on the core principles that define object oriented programming?  I've heard everywhere from 3 to 7.  In a field of research about a synthetic concept, an authoritative answer is almost impossible to obtain.

Programmers were then forced to study to the interview.  Careercup is one of my favorite sites for this.  This almost …

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…