A couple of days ago I posted that, although some developers use the terms Inversion of Control and Dependency Injection as if they are the same thing, they are in fact not the same. I said that Dependency Injection was a "kind of" Inversion of Control, it wasn't the only kind and they are not the same thing. I then went on to give an explanation of the Dependency Injection pattern.
Well it turns out that all the developers who thought they were the same thing all read my blog (hey, who knew?) and most of them felt the need to email me telling me I was wrong and challenging me to give an example of Inversion of Control that was not Dependency Injection.
Well okay, fair enough. The other kind of Inversion of Control is based on program control flow and to cut a long story short, it is basically the event based programming model. Here's an example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IOC2
{
class Program
{
static void Main(string[] args)
{
//GS - Get the User's name
Console.WriteLine("Please enter your name:");
string input = Console.ReadLine();
//GS - Say hello
Console.WriteLine("Hello " + input);
//GS - Wait so the user can see what was written
//to the console
Console.ReadLine();
}
}
}
In the above example the control flow of the program is very simple. A user is asked to input their name and then the program prints out a personalised greeting on the console. Now in Inversion of Control, in relation to control of flow; instead of the programmer specifying a series of events to happen they would register desired responses to particular events and then let some external entity (the user perhaps) take control of the order of these events. In other words, the programmer implements an event driven programming model, as in the following example.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IOC3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGreeting_Click(object sender, EventArgs e)
{
//GS - Say hello
MessageBox.Show("Hello " + txtUserName.Text);
}
private void btnClose_Click(object sender, EventArgs e)
{
//GS - Close the window
Close();
}
}
}
In the example above, you can see that the flow of the program is now not obvious, as it was before. The user may write his name in the textbox, he may change it a bunch of times before pressing the "Greeting" button, or he may not press it at all, but instead press the "Close" button. There is now no way for the programmer to know what the control flow is going to be, the contol flow as "suffered" Inversion of Control.
As you can see, the above is an example of Inversion of Control that does not use Dependency Injection. I stand by what I said in my previous post. Dependency Injection is a "kind of " Inversion of Control, but it's not the only kind, and the two things are not the same.