Inversion of Control and Inversify

Osusara Kammalawatta
3 min readJul 28, 2020

--

The Inversion of Control (IoC) and Dependency Injection (DI) are all about removing dependencies from your code which makes a system more decoupled and maintainable. The main goal of this article is to explain the things above mentioned and what is InversifyJS.

Source: https://www.liveabout.com/strategies-for-the-4-x-100-relay-race-3259122

Inversion of Control (IoC)

IoC is a principle. In traditional software development, our code controls the flow. Whenever we need help from a third-party library, we plug them to our code and call them. On the other hand when we use a framework, it does the most of the work. When it’s time to our application logic, framework hand over the control flow to our code. After finishing it again our code returns the control to the framework.

Maybe this is not the exact definition of the Inversion of Control. I just explained as simple as possible using the difference between libraries and frameworks.

In object-oriented programming, there are several basic techniques to implement Inversion of Control. They are,

  • using a factory pattern
  • using a service locator pattern
  • using dependency injection

Dependency Injection (DI)

I found a really good little bit funny explanation on StackOverflow for this.

When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has expired.

What you should be doing is stating a need, “I need something to drink with lunch,” and then we will make sure you have something when you sit down to eat.

Technically, instead of doing this,

public SomeClass() {
myObject = Factory.getObject();
}

we do this. We pass the object as a parameter.

public SomeClass (MyClass myObject) {
this.myObject = myObject;
}

Basically, we provide the objects that an object needs (its dependencies) instead of having it construct them itself.

Source: https://www.freecodecamp.org/news/a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f/

There are three ways to do the dependency injection,

  • constructor injection: the dependencies are provided through a class constructor.
  • setter injection: the client exposes a setter method that the injector uses to inject the dependency.
  • interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.

Advantages of DI

  1. Helps in Unit testing.
  2. Boilerplate code is reduced, as initializing of dependencies is done by the injector component.
  3. Extending the application becomes easier.
  4. Helps to enable loose coupling, which is important in application programming.

Disadvantages of DI

  1. Increases complexity.
  2. Many compile-time errors are pushed to run-time.

InversifyJS

InversifyJS works as an Inversion of Control (IoC) container for TypeScript and JavaScript apps. An IoC container allows us to achieve IoC by using dependency injection. More specifically constructor dependency injection. It uses a class constructor to identify and inject its dependencies. InversifyJS has a friendly API and encourages the usage of the best OOP and IoC practices. Also it is light weight.

Here’s how to use InversifyJS to achieve Dependency Injection. Also check out the official website of InversifyJS. Hope you got some thing from my article.

Happy Coding Folks 👽

--

--

Osusara Kammalawatta
Osusara Kammalawatta

Written by Osusara Kammalawatta

Developer 💻| Designer 🖥️| Content Creator 📹

Responses (1)