Diving into Dependency Injection (DI), or How to Crack the Matrix

A long time ago in a distant Galaxy, when the Wachowski sisters were still brothers, artificial intelligence in the person of the Architect enslaved humanity and created the Matrix ... Hello everyone, this is Maxim Kravets from Holyweb again, and today I want to talk about Dependency Injection, that is, about dependency injection, or just DI. What for? Perhaps you just want to feel like Morpheus, saying the sacramental: "I can't explain to you what DI is, I can only show you the truth."  





Formulation of the problem

Here. Take a look at these birds. There is a program to manage them. Other programs control trees and wind, sunrise and sunset. The programs are being improved. They all do their own part of the job.





Pythia





, , —  , . , . —  ? , . , ?





? , , , .





, , .





? , (, ), (Injection) , , , . 





, , : , ? — (Dependency) .





? — . Dependency Injection — . — , ? 





, : « ». — . . ? . 





, - — , ! 





. , , , ! — , .





, . , , . DI — . . matrix, , . , , , , , , whoWin():





class Matrix {
  agent = {
    name: 'Smith',
    damage: 10000,
  };

  human = {
    name: 'Cypher',
    damage: 100,
  };

  whoWin(): string {
    const result = this.agent.damage > this.human.damage
      ? this.agent.name
      : this.human.name;
    return result;
  }
}

const matrixV1 = new Matrix();
console.log(‘ ’, matrixV1.whoWin());
      
      



, , , .





  Smith
      
      



, , , , - — . , , . — . , . .





class Human {
  name;
  damage;

  constructor(name, damage) {
    this.name = name;
    this.damage = damage;
  }

  get name(): string {
    return this.name;
  }

  get damage(): number {
    return this.damage;
  }
}

class Matrix {
  agent = {
    name: 'Smith',
    damage: 10000,
  };
 human;

  constructor(challenger) {
    this.human = challenger;
  }

  whoWin(): string {
    const result = this.agent.damage > this.human.damage
      ? this.agent.name
      : this.human.name;
    return result;
  }
      
      



Human, , . — . , ?





const Trinity = new Human('Trinity', 500);
const matrixV1 = new Matrix(Trinity);
console.log(' ', matrixV1.whoWin());
      
      



, « » (), , , .





  Smith
      
      



! , ? , Matrix Human! , , , !





class Human {
  get damage(): number {
    return this.damage * 1000;
  }
}
      
      



...





?

? , Matrix challenger, , damage, . , , ! — . ? , damage, power? strength?





! Dependency inversion principle, (DIP). , , , «» , Dependency inversion (DI), .





, :





  1. . .





  2. . .





? , , , .





Matrix AbstractHuman, Human — :





abstract class AbstractHuman {
  abstract get name(): string;
  abstract get damage(): number;
}

class Human implements AbstractHuman{
  name;
  damage;

  constructor(name, damage) {
    this.name = name;
    this.damage = damage;
  }

  get name(): string {
    return this.name;
  }

  get damage(): number {
    return this.damage;
  }
}


class Matrix {
  agent = {
    name: 'Smith',
    damage: 10000,
  };
 human;

  constructor(challenger: AbstractHuman) {
    this.human = challenger;
  }

  whoWin(): string {
    const result = this.agent.damage > this.human.damage
      ? this.agent.name
      : this.human.name;
    return result;
  }
}

const Morpheus = new Human('Morpheus', 900);
const matrixV2 = new Matrix(Morpheus);
console.log(' ', matrixV2.whoWin());
      
      



, — .





  Smith
      
      



, ? Matrix Human — . Human , — «» AbstractHuman () , . .





, ! , , — … , .





      Smith
      Smith
      
      



, , , . , :





...
class TheOne implements AbstractHuman{
  name;
  damage;

  constructor(name, damage) {
    this.name = name;
    this.damage = damage;
  }

  get name(): string {
    return this.name;
  }

  get damage(): number {
    return this.damage * 1000;
  }
}
const Neo = new TheOne('Neo, 500);
const matrixV5 = new Matrix(Neo);
      
      



!





      
      
      



, ? Matrix, Human, . . — . 





, , , , . , , !





, , — Inversion of Control (IoC). 





, , (, ) (, ). ( ) — . 





, DIP ( ) — IoC.





- -?

—  . , singleton multiton — (), (). 





, .





  • ,





  • , ,





  • ( ),





  • , , .





: - / (Service Locator), - DI, IoC Container. .





, (). , . Angular —  Injectable.





@Injectable()
export class SomeService {}
      
      



IoC . 





SomeService, , . 





-, —

—  , . , , . , «» , , . , , .





—  , « » « - ». , . new, «», , .





, ?

, «» - , IoC?  





1 — .





  • , -. 





  • DI. .





  • , .





  • — , , «», — IoC . 





2 — .





  • — , -.





  • — , Y. 





  • , Y , X.





  • — , , . .





3 — .





  • — .





  • production — «» .





  • — , .





  • — , , .





, , , DI, . . —  , . , , , DI. 





If you have any questions or additions on the topic, I will be glad to continue communication in the comments. Write, what to tell about in the next article? And if you want to get to know us better , I am always in touch in Telegram @maximkravec








All Articles