IOC 控制反转 DI依赖注入

2025-09-11 00:00    #  

在学习nestjs 之前需要先了解其设计模式

IOC

Inversion of Control字面意思是控制反转,具体定义是高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。

DI

依赖注入(Dependency Injection)其实和IoC是同根生,这两个原本就是一个东西,只不过由于控制反转概念比较含糊(可能只是理解为容器控制对象这一个层面,很难让人想到谁来维护对象关系),所以2004年大师级人物Martin Fowler又给出了一个新的名字:“依赖注入”。 类A依赖类B的常规表现是在A中使用B的instance。

案例未使用控制反转和依赖注入之前的代码

 1class A {
 2    name: string
 3    constructor(name: string) {
 4        this.name = name
 5    }
 6}
 7 
 8 
 9class B {
10    age:number
11    entity:A // 属性 entity A 类型
12    constructor (age:number) {
13        this.age = age;
14        this.entity = new A('小满')
15    }
16}
17 
18const c = new B(18)
19 
20c.entity.name

我们可以看到,B 中代码的实现是需要依赖 A 的,两者的代码耦合度非常高。当两者之间的业务逻辑复杂程度增加的情况下,维护成本与代码可读性都会随着增加,并且很难再多引入额外的模块进行功能拓展。

为了解决这个问题可以使用IOC容器

 1class A {
 2    name: string
 3    constructor(name: string) {
 4        this.name = name
 5    }
 6}
 7 
 8 
 9class C {
10    name: string
11    constructor(name: string) {
12        this.name = name
13    }
14}
15// 中间容器
16//中间件用于解耦
17class Container {
18    modeuls: any
19    constructor() {
20        this.modeuls = {}
21    }
22
23    // 提供 对象 ,放入容器
24    provide(key: string, modeuls: any) {
25        this.modeuls[key] = modeuls
26    }
27    get(key) {
28        return this.modeuls[key]
29    }
30}
31 
32const mo = new Container()
33mo.provide('a', new A('小满1')) // 放入对象
34mo.provide('c', new C('小满2'))
35 
36class B {
37    a: any
38    c: any
39    // 从容器里面得到 想要的对象
40    constructor(container: Container) {
41        this.a = container.get('a')
42        this.c = container.get('c')
43    }
44}
45 
46new B(mo)

其实就是写了一个中间件,来收集依赖,主要是为了解耦,减少维护成本