Adapting to Inversion of Control and Dependency Injection
Posted by Filip Ekberg on December 20 2011 11 Comments
Posted by Filip Ekberg on December 20 2011 11 Comments
You might have come across the phrases IoC, Dependency Injection, Mocking among others, these are commonly used when talking about “Inversion Of Control” which is the full meaning of the abbreviation IoC. So what is this “IoC” that everyone is talking about?
Inversion of control is a principle in Software Engineering, let’s just take a look at the Wikipedia definition of this
In practice, Inversion of Control is a style of software construction where reusable generic code controls the execution of problem-specific code. It carries the strong connotation that the reusable code and the problem-specific code are developed independently, which often results in a single integrated application.
Rather than explaining the text below, let me show an example of an application that does not follow this principle, but will when we’re done here! I’ve got a solution prepared that looks like this:
This solution simulates an order process, where we’ve got a window where we can place some kind of order and then we’ve got some sort of payment provider that executes the order request. This is the only content so far in the InversionOfControl.Payment library:
{
Success,
Failure
}
public class PayPalPaymentProvider
{
public PaymentResult Execute(string paymentData)
{
if (string.IsNullOrEmpty(paymentData)) return PaymentResult.Failure;
return PaymentResult.Success;
}
}
All we’re doing is to check if the data we send into the execute method actually contains any information at all, if it doesn’t we return a failure result, otherwise we return a success result. Keep in mind that when you implement communication with real payment providers, there are a lot more code to it. But let’s keep the example clean!
The order form is very simple and looks like this:
When we press the button this is what’s going to happen:
{
ProcessPayment(orderInformation.Text);
}
public void ProcessPayment(string orderData)
{
var paymentProvider = new PayPalPaymentProvider();
var result = paymentProvider.Execute(orderData);
MessageBox.Show(result.ToString());
}
So far we’ve got our order form where we can place an order, when we place the order we execute our order placement and directly communicate with our payment provider. Let’s call this Step 1, we’ve set up the project and we can execute a payment, you can download the code here, unzip it and look in the folder named Step 1. You can also browse the code on github.
Let’s bring up another one of those commonly used abbreviations; TDD or Test After Development. Payments are a great example of something that really needs to be tested thoroughly and you can’t have any errors here. Because either it will be expensive for you, your customer or the end user, no matter which one, you’ve got a big problem.
But what happens when we introduce testing at this stage? How do we actually test the entire flow of the application without communicating directly with payment provider? This is where Inversion of Control and Dependency Injection comes into play. Dependency Injection is a design pattern with the following Wikipedia explanation
DI whose purpose is to improve testability of, and simplify deployment of components in large software systems.
The pattern involves some rules that we need to adapt to, which basically means we have to write cleaner and more testable code. In order to make our code cleaner and more testable we need to break some of the static parts out, this being the actual instantiation of a concrete type in the payment processing.
Take a look at this line of code:
There’s something “wrong” with it; We’re instantiation the concrete type here! To make the method testable, we don’t want that because this means that it will always use our real payment provider information and execute the order against it, which can make the testing expensive for the tester ( if he has to provide is credit card number all the time that is! ).
The easiest way to break it out is to move this to the method argument list, which means that instead of creating an instance in the method, we want someone else to inject it to us. Let’s look at this in a two-step refactoring, first we move the declaration outside of the context and change the method signature:
{
var result = paymentProvider.Execute(orderData);
MessageBox.Show(result.ToString());
}
This will let us create an instance of PayPalPaymentProvider before we call the processing method and inject it when we call the method. But we are still working with the concrete type, so let’s take a look at the next step in the two-way refactor, let’s simply it to an interface!
All the interface need to tell us is that we’ve got a method called Execute that will return a result to us, so we simply declare it like this:
{
PaymentResult Execute(string paymentData);
}
And then we need to tell our payment provider to actually implement the interface, all we need to change is the class signature:
So far so good!
Now we just need to change the method signature for our payment processing as well and we are all set for some DI!
Now when we want to process the payment when we execute the order, the event handler will look like this instead:
{
var paymentProvider = new PayPalPaymentProvider();
ProcessPayment(orderInformation.Text, paymentProvider);
}
By the looks of it, we now have a simple and elegant way to process our payments, let’s create a test project and reference to InversionOfControl.Demo. Now create a test called PaymentTests.
So how do we start testing the payment processing?
We only have one concrete payment provider at the moment and this one we don’t want to use for our unit testing, so what we will do instead is that we will create a fake class that will act as the real payment provider, this is dependency injection! So in our payment project, add a new class called FakePaymentProvider and just have it look somewhat like this:
{
public PaymentResult Execute(string paymentData)
{
if (paymentData == "OK") return PaymentResult.Success;
return PaymentResult.Failure;
}
}
Now the test itself isn’t that hard, we’ve already covered the hardest part and that was doing the actual refactoring. So, this is what my test ended up looking like:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class PaymentTests
{
private IPaymentProvider paymentProvider;
[TestInitialize]
public void Initialize()
{
paymentProvider = new FakePaymentProvider();
}
[TestMethod]
public void OrderFailsWithEmptyInformation()
{
var result = paymentProvider.Execute(null);
Assert.AreEqual(result, PaymentResult.Failure);
}
[TestMethod]
public void OrderSucceedsWithOKInformation()
{
var result = paymentProvider.Execute("OK");
Assert.AreEqual(result, PaymentResult.Success);
}
}
I’m initializing my test by creating a fake payment provider and then I am testing if it fails and succeeds as I expect it to, we’ve got a nice way to test our code! This brings us to the next problem, where Dependency Injection and Inversion of Control meet each other, what if I want to test the flow of my application, without actually changing the concrete instantiations everywhere to FakePaymentProvider?
This is where we’re going to bring in a library to help us along the way, but before we do that, let’s just talk a little bit about what is going to happen.
So we’ve got a system that is now refactored so that the important methods that we want to make testable no longer use the concrete types as parameters but rather interfaces that we can implement to create our fake repositories or providers of some sort. The next thing we want to remove completely is the way that we create an instance of our concrete type before we invoke the payment provider.
To do this, we’re going to use a library that let’s us define what kind of implementation to use in runtime. To do this, I’m going to take some help of NuGet to install a library called Ninject. Ninject is an open source dependency injector for .NET and it’s Very good!
Fire up NuGet and let’s start installing those packages!
All you need to write is the following:
Be sure that you’ve got “InversionOfControl.Demo” selected as “Default project” and you should see something like this:
There are a lot of different type of dependency injectors out there and they act a little bit different. Ninject works like this: you tell ninject what types you want bound to what types or interfaces and then you request these types. There are other dependency injectors that will just replace all interfaces with the concrete type of your choice, but I like Ninject because it gives us an extra type of abstractness and there’s not that much magic to it.
Basically we need to know two things in order to get started with Ninject ( There’s a great Wiki if you want to know more ) first of all, you use something called “Modules” to easily define different types of bindings. For instance, we’re going to create a module that is called PaymentProviderModule. The second thing is that you use something called a kernel to retrieve the concrete types and you tell the kernel which module it should use.
Let’s start off by creating the module:
using InversionOfControl.Payment;
public class PaymentProviderModule : NinjectModule
{
public override void Load()
{
Bind<IPaymentProvider>().To<FakePaymentProvider>();
}
}
All we’re saying here is that each time we ask for an IPaymentProvider we’re going to get a Fake payment provider. Now let’s take a look at how this changes the rest of the code! Let’s head over to the order button event handler. The only thing that actually changes now is how we retrieve a concrete type of IPaymentProvider and this is how we do that with Ninject:
Notice this part in particular kernel.Get<IPaymentProvider>(), we’re actually requesting the interface and not a concrete type! Let’s run the application and see what happens, if we enter anything else than “OK”, we should see a message box that says “Failure”:
And it does!
We’ve successfully made our application less dependent on the concrete type and we’ve introduced testability which we had no way of doing before! The example code for the payment provider is as stated before a bit thin, but in a real world scenario the fake payment provider might not be much bigger than the one we’ve got here, but the real concrete type might.
Now let’s take a look at that definition of Inversion of Control again; it basically means that our application should be able to execute anything against any payment provider without ever having to know about its implementation! We’ve only scratched the surface on how this helps out with testability, there are great frameworks that will help us even more without having to actually create a new concrete type for testing, which in this case was our fake payment provider.
As you might see this is very powerful and gives us a great way to make our applications more testable which will make them more reliable. You can look at the code at the github repository. You can also download the code here.
I hope you found this interesting, if you have any thoughts please leave a comment below!
?>




Filip Ekberg is a Senior Software Engineer working in the country with all the polar bears, author of a self-published C# programming book and overall in love with programming.





This is NOT Inversion of Control, the class is clearly responsible for creating it’s own dependency (indirectly though the kernel), thus control is not inverted at all.
Also, what’s with the unit test? it only test FakePaymentProvider which is obviously not necessary.
Hi Luke,
This is just the beginning, I am just scratching the surface. Yes I am creating the instance of the PaymentProviderModule in the class that I want to make less dependent, in an ideal case you would inject this into the constructor instead. You could also use a DI-framework that don’t require you to create an instance of anything like I’ve done here.
I’ve tried to keep it simple and focus on the important part, which is about why it’s important to have low coupling. Same with the unit test, this sould of course in real life test process payment method which is essentially the most important method in our case.
But yes, you are right, in this case the class is still responsible for creating the dependency, thus we’ve only inverted the control for certain parts of the system and to get even lower coupling we would need to remove the knowledge of any other modules than the standard one in the class.
Also keep in mind that DI/IoC don’t just have a single purpose, making code testable is just one of the benefits.
Luke, really sorry for this but are you a wiseacre?
The best way to describe a problem, technique or whatever is to use small steps. Clearly this article have to start somewhere?
Sorry if I didn’t realize this was the beginning of a multi part entry. I currently have to deal with production code like the above so I’m somewhat sensitive to seeing people spread misconceptions about IOC.
I think a better approach would have been to refactor with constructor injection first. Adding the IPaymentProvider isn’t really necessary at for IOC or unit testing.
Thanks for the work you put into this article but I’m afraid I have to partially agree with Luke.
I would avoid showing an example of using a DI framework as pseudo-service locator without more explanation. You are just going to confuse people (IMHO). In fact, when I teach IOC I don’t show classes/instances and related dependencies. I simply show examples using callbacks and simple functions to get the concept across and then expand from there.
Hi Luke, No problem. I think I will have to do at least one post more where I introduce another payment provider as well, so that it is more clear why I’ve simplified it to an interface. So that I can also go into how you would lower the coupling more by refactoring out the knowledge of the DI framework in the particular class. I don’t agree with you that this is a misconception about IoC, because it is essentially about making parts of your code less coupled and we’ve acheive that in the article, but not on a class level because we introduce another dependency.
Hi Mark,
Yes, you’re right that it can confuse people, but at an abstract level of it all, we did lower the coupling and got ourself a first drawft of what later can be refactored even more. I could have introduced a DI-framework that my code didn’t depend on at all, but I think that could have confused even more.
A few questions..
1) do you really test your “fakes” in production code? This seemed strange to me… I’d have only tested the real PaymentProvider (using unit tests) and the OrderForm (manually, using the fake).
2) where would you put the kernel.Get calls in production code? Does every click-event handler call kernel.Get (i.e. do your forms have a dependency on the IoC container?) or do you put these calls somewhere else (like outside the form + constructor injection)?
Hi Max,
No you don’t need to test your fakes, the article might have been a bit unclear about that. I use the fakes in order to test the other behaviors that my “real” implementation doesn’t need to be there for.
I’d much rather do constructor injection, but I don’t see it being a big issue adding the kernel.Get-calls in the methods that require the implementation. It is very individual what you like here but I would go for constructor injection!
Hi Filip
I think that you need to be clearer about the distinction between Inversion of Control and Dependency Injection.
You are only using Dependency Injection to inject IPaymentProvider into the method that does the work making it testable. However, I do not think that this is Inversion of Control.
At the end of your demo I am unsure who “owns” the IPaymentProvider. If it is defined in the same assembly as the PaymentProvider class then you are not inverting the control structure; i.e. the Presentation Layer references the Service Layer, the Service Layer references the Data Layer etc.
If the interface is defined in the InverstionOfControl.Payment assembly:
{
public interface IPaymentProvider
{
PaymentResult Execute(string paymentData);
}
}
Then the InversionOfControl.Demo project has a dependency on it. Should I create a website and as part of that website I need to change the PaymentResult to being a class (which includes a redirect URI to the PayPal login page) then I must re-compile and alter the InversionOfControl.Demo project.
In this case I would personally define the projects InversionOfControl.Demo.Interface and InversionOfControl.Web.Interface:
{
public enum PaymentResult { Success, Failed }
public interface IPaymentProvider
{
PaymentResult Execute(string paymentData);
}
}
namespace InversionOfControl.Web.Interface
{
public enum PaymentResultStatus { Success, Failed, Redirect }
public class PaymentResult
{
public PaymentResultStatus Status { get; set; }
public string RedirectUri { get; set;
}
public interface IPaymentProvider
{
PaymentResult Execute(string paymentData);
}
}
Any common payment provider project can depend on those two interfaces:
{
public class PayPalPaymentProcessor : InversionOfControl.Web.Interface.IPaymentProcessor, InversionOfControl.Demo.Interface.IPaymentProcessor
{
// Implementations go here
}
}
Hi Ian,
Thanks for your input!
I will make a follow up article that I dig deeper into this subject to make the distinction between the different principles more clear.
Thanks, It is really a nice post.
Can you please visit this two post of mine and comment about what you feel.
An idea of a three tire application using IoC(Inversion of Control), DI(Dependency Injection) and perfectly decoupled layers
and
A simple container used in mvc web applications with DI(Dependency Injection)