nestjs 如何工作并运行带参数的构造函数 [英] How nestjs works and runs constructors with parameter

查看:162
本文介绍了nestjs 如何工作并运行带参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 nestJS 的新手,也找不到太多关于它的信息,所以我也不敢深入研究它的源代码(也是打字稿的初学者).我真的很想知道这些东西是如何工作的,我将在下面提到

I'm new at nestJS and also can't find too much information about it so I also afraid to dive into source code it (also a beginner in typescript). I really wanna know how those stuffs work which I'll mention below

首先:我正在看教程,其中我们有名为 Task 的服务

First: I'm watching the tutorial where we have service called Task

export class TasksController {
  constructor(private readonly tasksService: TasksService) { }

  Get()
  index() {
     this.tasksService.all();
  }
}

这里 TasksService 仅用作类型,我猜不是用作类.在普通的打字稿中,我会写这样的东西

Here TasksService used as just a type, not as a class I guess. In normal typescript, I would write something like that

let task = new TasksController(new TasksService())

然后我就可以调用this.tasksService.all();

第二:@Injectable()@Inject()@InjectRepository() 等如何工作.原始文档有点混乱:(

Second: How @Injectable(), @Inject(), @InjectRepository() etc works. Original docs are bit confusing :(

推荐答案

Nest it 建立在 Angular 的思想之上它处理了很多依赖注入关注点分离.这些想法来自更多面向对象的语言,如 Java 和 C++,尤其是来自 Spring/SpringBoot 等框架.

Nest it built on the ideas of Angular which deals a lot with Dependency Injection and Separation of Concerns. These ideas come from more Object Oriented languages like Java and C++ and especially from frameworks like Spring/SpringBoot.

除此之外,Nest 通过连线"做了很多事情.提供者一起允许开发人员不必担心实例化每个类,而是让框架为他们处理.我先谈谈你的第二点,希望它有助于阐明第一点.

With that out of the way, Nest does a lot of stuff by "wiring up" providers together to allow for developers to not worry about instantiating each class, and rather let the framework handle that for them. I'll talk about your second point first and hopefully it will help shed some light on the first point.

打字稿中的所有装饰器都用于设置元数据.然后 Nest 将读取此元数据并做出相应的响应.在大多数情况下,这一切都由 DI 系统的设置方式处理.

All decorators in typescript are used to set metadata. Nest will then read this metadata and respond to it accordingly. For the most part, this all is taken care of under the hood with how the DI system is set up.

  • @Injectable() 告诉 Nest 嘿,这个类是一个提供者*,因此它应该能够将值注入其中并能够注入其他提供者.您的所有服务都将标记为 @Injectable() 以及一些特殊的类.

  • @Injectable() tells Nest "Hey, this class is a provider* and as such it should be able to have values injected into it and be able to be injected into other providers. All of your services will be marked as @Injectable() along with a few special classes.

@Inject() 是一个接收注入令牌**的装饰器.这告诉 Nest 嘿,我想注入与我刚刚给你的令牌相关的提供者.创建一个实例并将其注入这里.令牌可以是字符串或符号,但它必须是唯一的(即不与其他提供者冲突).使用可以多次使用同一个令牌.

@Inject() is a decorator that takes in an injection token**. This tells Nest "Hey I want to inject the provider tied to the token I just gave you. Make an instance and inject it here. The token can be a string or a symbol, but it needs to be unique (i.e. not clash with another provider). Use can use the same token multiple times.

@InjectRepository()/@InjectModel() 这些是使用标准 的特殊 @Inject() 装饰器>@Inject() 装饰器使注入令牌与 Nest 已经在 TypeormModuleMongooseModule

@InjectRepository()/@InjectModel() these are special @Inject() decorators that use the standard @Inject() decorator under the hood to keep the injection token in line with the injection tokens Nest already creates in the TypeormModule and the MongooseModule

现在我们对装饰器设置的元数据有了更多的了解,让我们来谈谈 Nest 如何解决依赖关系.Nest 的作用是扫描每个类并查找元数据是否为 ​​@Injectable().(@Controller() 和其他装饰器确实设置了这个).然后它查看构造函数中的类并找出该类是否具有注入标记(除非另有说明,否则类仅由名称确定***).如果没有,它会检查是否有一个 @Inject() 装饰器并找到它要使用的特定值.如果它找到一个类,它将实例化它,将其保存在缓存中,并将其提供给该类.如果是值,则直接提供给类.

So now that we know a bit more about the metadata that the decorators are setting, let's talk about how Nest resolves dependencies. What nest does is it scans each class and finds metadata on if it is @Injectable() or not. (@Controller() and other decorators do set this). Then it looks at the classes in the the constructor and finds out if that class has an injection token (classes are just determined by name unless otherwise mentioned***). If not it checks if there is an @Inject() decorator for it and finds its specific value to use. If it finds a class it will instantiate it, save it in a cache, and provide it to the class. If it is a value, it is directly provided to the class.

要更深入地了解 DI 以及其他框架如何处理它.Angular 是一个很好的资源,因为正如我所说,Nest 从中获得了很多灵感

To get more in depth, rally read up on DI and how other frameworks handle it as well. Angular is a great resource because that, as I said, is where Nest got a lot of inspiration from

*例外包括防护、拦截器、管道和过滤器.虽然这些是 @Injectable(),但它们不是普通的提供者.

*exceptions include Guards, Interceptors, Pipes, and Filters. While these are @Injectable() they are not normal providers.

** 这是真的,除非您正在处理类成员注入,在这种情况下您不提供令牌.

** this is true unless you are dealing with class member injection in which case you don't provide a token.

*** 您可以使用自定义提供程序进行管理

*** you can manage this with custom providers

这篇关于nestjs 如何工作并运行带参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆