Inversion of Control (IoC)

Inversion of Control (IoC) moves responsibility for orchestration or dependency creation outside the component doing the work.

Think about it like this

Normally, when you write code, you’re the one in control. You make all the decisions about what happens and when. But with IoC, you’re basically saying “hey, I’m going to let someone else take the wheel for a bit”. And who’s that someone else? Your dependencies!

So, how does it work?

Well, let’s say you have a class that needs to use another class to do some work. Normally, you’d create an instance of that class inside your original class and call its methods directly. But with IoC, you’re going to let someone else handle that for you. Maybe you’ll use a DI container or a framework to manage your dependencies, or maybe you’ll just pass them in as parameters. (doesn’t it sound familiar? 🤔)

Example

Let’s say you have a Car class that needs an Engine to run:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Car {
  engine: Engine;

  constructor() {
    this.engine = new Engine();
  }

  start() {
    this.engine.turnOn();
  }
}

In this example, we’re creating a new instance of Engine inside the Car constructor.

But with IoC, we could do something like this instead:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Car {
  engine: Engine;

  constructor(engine: Engine) {
    this.engine = engine;
  }

  start() {
    this.engine.turnOn();
  }
}

const myEngine = new Engine();
const myCar = new Car(myEngine);
myCar.start();

Now, we’re passing the Engine instance into the Car constructor instead of creating it inside the class. This gives us more control over how our dependencies are managed and makes our code more flexible.

Weaknesses

  1. Complexity: IoC can be like a Rubik’s cube - fun to play with at first, but frustrating when you can’t figure out how to solve it. And unlike a Rubik’s cube, there’s no guidebook for IoC that tells you exactly what to do.

  2. Performance: container startup, reflection, or a large dependency graph can add measurable overhead. Profile the real application before treating IoC as the cause.

  3. Configuration: Setting up IoC can feel like trying to assemble IKEA furniture without instructions. You’re not sure which pieces go where, and by the time you’re done, you have a few extra screws left over.

  4. Learning curve: IoC can be like learning a new language - at first, everything sounds like gibberish. But with practice, you start to understand the syntax and grammar, and before you know it, you’re speaking fluently.