前言
在 NestJS 中,依赖注入是一个核心概念,用于管理应用程序的组件和服务之间的依赖关系。 这种模式通过提供一个容器(NestContainer)来解决类之间的依赖 关系, 使得代码更加模块化、可测试且易于维护。
Dependency Injection system of Nest.js
Introduction
-
The Dependency Injection(DI) pattern
- is frequently used today by the bigest framework.
-
It is a way
- to keep code clean and easier to use.
-
By using this pattern
- you end up with fewer coupled components
- and more reusable ones
- which helps accelerate the development process time
-
Nest.js is based on Angular in terms of architecture
-
is used to create
- testable
- scalable
- lossely-coupled
- easily maintainable application
-
is used to create
-
As is the case with Angular
- Nest.js has ite own dependency injection system
- which is part of the core of the framework
- meaning that Nest.js is less dependent on a third-party library.
- Nest.js has ite own dependency injection system
翻译
-
依赖注入模式
- 目前最大的框架都京城使 用这种模式
- 这是一种保持代码简洁、更易于使用的方法
- 使用这种模式可以减少耦合组件的数量,增加可重用组件的数量,从而加快开发进程
-
Nest.js在架构上基于Angular
- 用于创建:可测试、可扩展、松耦合和易维护的应用程序
- 与Angular一样,Nest.js也有自己的依赖注入系统
- 这是架构核心的一部分
- 这意味着Nest.js对第三方库的依赖性较低
知识点
-
dependency injection pattern
- 依赖注入
-
frequently used today
- 现今经常被使用
-
keep code clean
- 保持代码整洁
-
easier to use
- 更易于使用
-
fewer coupled components
- 较少的耦合组件
-
more reusable components
- 更多可重用组件
-
accelerate the development process time
- 加速开发过程
-
based on Angular
- 基于...
-
in terms of architecture
- 在架构方面
-
create testable, scalable, loosely-coupled and easily maintainable application
- 创建可测试、可扩展、松耦合且易于维护的应用程序
-
has its own dependency injection system
- 拥有自己的依赖注入系统
-
part of the core of the framework
- ...的核心部分
-
less dependent on a third-party library
- 较少依赖第三方库
Overview of Dependency Injection
-
Since TypeScript 1.5
- introduces the notion of the decorator
-
you can do meta-programming
- using the added metadata provided by using a decorator on different objects or properties
- such as
- class
- function
- function parameters
- class property
- such as
- using the added metadata provided by using a decorator on different objects or properties
-
meta-programming
-
is the ability
- to write some code or program
- using the metadata describing on object
- to write some code or program
-
is the ability
-
The type of program
- allows you to modify the functioning of a program
- using its own metadata
- this metadata is of interest to us
- because it helps inject some object into another object
- whose name is Dependency Injection
- using its own metadata
- allows you to modify the functioning of a program
-
By using the decorator
- you can add metadata on any object or property linked to those decorators
- To get or define metadata on any object
- you can also use the
reflect-metadata
library in order to manipulate them
- you can also use the
翻译
-
Typescript 1.5
- 引入了装饰器的概念
-
Typescript Meta-programming
- 是指使用描述对象的元数据编写代码或程序的能力
- 这类程序允许你使用程序本身的元数据修改程序的功能
- 可以在不同的对象上或属性(类,函数、函数参数、类属性)上使用装饰器提供的附加元数据进行元编程
- 我们对元数据很感兴趣
- 是因为它有助于将某个对象注入到另一个对象中,这就是所谓的依赖注入
- 是指使用描述对象的元数据编写代码或程序的能力
-
通过装饰器
- 你可以再与这些装饰器链接的任何对象或属性上添加元数据
- 这将定义使用装饰器的对象类型
- 但也可以定义函数所需的所有参数
- 这些参数的元数据中都有描述
- 要获取或定义任何对象的元数据
- 可以使用
reflect-metadata
库来操作它们
- 你可以再与这些装饰器链接的任何对象或属性上添加元数据
知识点
Why use Dependency Injection
-
The real interest in using Dependency Injection
- is that the object will be less coupled between the dependent and its dependencies
-
With the framework
- that provides the injector system
- you can manage your objects without thinking about the instanciation fo them
- because that is managed by the injector
- which is there to resolve the dependencies of every dependent object.
- you can manage your objects without thinking about the instanciation fo them
- that provides the injector system
-
This means it is easier
- to write test and mock dependencies
- which are much cleaner and more readable
翻译
- 使用依赖注入的真正好处在于
- 对象与依赖对象之间的耦合度会更低
- 有了提供注入系统的框架,你就可以管理你的对象,而无需考虑对象的实例化
- 因为这是注入器管理的,注入器会解决每个依赖对象的依赖关系
- 这意味着更容易编写测试和模拟依赖关系
- 从而使测试更简洁、可读性更高