Constructor Injection in Dependency Injection

Constructor injection supplies required dependencies when an object is created. It is a useful default because a successfully constructed instance has everything it needs to operate.

Let’s say we have a simple example of a Computer class that depends on another class, CPU. We can use construction injection to provide the CPU class to the Computer constructor like so:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class CPU {
  public brand: string;

  constructor(brand: string) {
    this.brand = brand;
  }
}

class Computer {
  public cpu: CPU;

  constructor(cpu: CPU) {
    this.cpu = cpu;
  }
}

const amd_cpu: CPU = new CPU("AMD");
const computer: Computer = new Computer(amd_cpu);

With construction injection, our Computer class is no longer responsible for creating its own dependencies. Instead, it’s provided with everything it needs to work correctly.