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.
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
Post a Comment