Programming

IoC – Dependency Injection

In this post you will learn what Dependency Injection is, why it would be useful and how to implement it.

The examples will be written in C# and published into my GitHub.

I chose C# because it’s a fairly popular language and will (hopefully) be understood by anyone from any language.

Let’s get right in.

What is Dependency Injection

Dependency Injection is a Inversion of Control technique used to “inject” a dependency (class or a whole API) onto a class, allowing for the dependency to be changed at runtime. (for instance by user input)

Suppose we have a class that looks like this:

Example of Non Dependency Injection

You can clearly see that this isn’t dependency “injectable”, since we, when calling this method, can’t specify which payment method to use, we’re forced to use Credit Card.

What would be an example of Dependency Injection then?

Example of Dependency Injection

In this case, we can change which payment method the method will use at run-time, by passing the interface dependency via parameter.

We would call it like this:

Using the PersonExample

Why would we want this?

There are quite some advantages of this, the ones I’m going to give might not be all of them but they’re certainly some of them.

  • Easier to unit test
    • By allowing us to change the dependency from the “outside” (when calling it), we can unit test it pretty easily, allowing us to easily mock it.
  • It’s a good alternative to the Singleton pattern
    • Static is usually seen as evil, and Singleton is no exception, being regarded as an anti-pattern.
  • Easier to maintain
    • Suppose you are using that first PersonExample in 50 different places, now imagine that you want to introduce a different IPaymentMethod, but only in 20 of those… How would you do that?
    • If you’re using Dependency Injection then it’s way easier, you would still need to change the code in 20 different places but it’s not as bad as when not using it.
    • If you were doing as the first example then you would probably need to pass a boolean or enum that would determine which payment method to use, yikes!
  • Decoupling from the class and its dependency
    • This is closely related to the previous, basically it allows us to re-use the class we’re writing more often, by making our class not be attached to the Dependency we’re using.

How to implement it?

You can find the code that demonstrates this at my GitHub: https://github.com/LegendL3n/Inversion-of-Control.

Keep in mind that in the GitHub example I’m passing the dependency via constructor.

However you can also pass it via parameter, setter or even by making the Person class implement an interface, forcing it to implement a setter to which the “injectors” will inject the payment method on.

Hopefully everything was clear, if you have any question or suggestion, you can either comment here or message me at my Twitter @L3nSec.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s