在NestJS中导入具有依赖注入的TypeScript模块 [英] Importing TypeScript modules with dependency injecetion in NestJS

查看:345
本文介绍了在NestJS中导入具有依赖注入的TypeScript模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的NestJS应用程序中-我有TypeScript类,这些类中已经注入了其他类和值.唯一的事情是,我要使用import语句导入TypeScript类,并使用DI系统注入它们.有什么方法可以删除导入语句并让DI系统处理它?<​​/p>

解决方案

TL; DR

  • import->类引用
  • DI->类实例
  • 可以通过字符串标记进行匹配,但是最好使用类引用.

封装

依赖注入系统主要处理类的实例化.这很好,因为您不必关心要注入的类所需的可传递依赖项.

示例:我想在我的UserController中使用UserService. UserService需要UserModel进行实例化.但是,此第二级依赖关系隐藏在UserController中.这很棒,因为当UserService获得像LoggingService这样的新依赖关系时,不必更改UserController.

所以不是

class UserController {
  constructor() {
    const userModel = new UserModel();
    this.userService = new UserService(userModel);
  }
}

你可以做

class UserController {
  // the transitive dependency on UserModel is hidden
  constructor(private userService: UserService) {}
}

类参考

但是,为了让DI知道要注入哪个服务,您需要从@Inject声明到实例化实际类的一些链接.当然,这种机制取决于DI系统的实现.引用可以是名称(字符串匹配),接口(DI决定使用哪种实现:UserService-> UserServiceImpl/MockUserServiceImpl),或者在默认情况下通过要实例化的类直接在nestjs中引用. /p>

尽管在nestjs中可以按名称进行匹配,但是按类进行匹配是首选,因为它使重构更加容易.

创建自定义提供程序时,您可以选择所需的令牌用于匹配.当您要注入值(没有用于匹配的类)时,这是必需的

const connectionProvider = {
  provide: 'Connection',
  useValue: connection,
};

@Module({
  providers: [connectionProvider],
})

或动态实例化的类.

const configServiceProvider = {
  provide: ConfigService,
  useClass: process.env.NODE_ENV === 'development'
    ? DevelopmentConfigService
    : ProductionConfigService,
};

@Module({
  providers: [configServiceProvider],
})

In my NestJS application - I have TypeScript classes that have other classes and values injected into them. The only thing is that I'm importing the TypeScript classes with import statements, and also using the DI system to inject them. Is there some way to remove the import statements and just let the DI system handle it?

解决方案

TL;DR

  • import -> class reference
  • DI -> class instantiation
  • Matching by string token is possible, but class reference is preferred.

Encapsulation

The dependency injection system mainly handles the instantiation of the classes. This is great, because you do not need to care about the transitive dependencies that the class you want to inject requires.

Example: I want to use the UserService in my UserController. The UserService requires the UserModel for instantiation. However, this second-level dependency is hidden in the UserController. This is great because when the UserService gets a new dependency like a LoggingService, the UserController does not have to be changed.

So instead of

class UserController {
  constructor() {
    const userModel = new UserModel();
    this.userService = new UserService(userModel);
  }
}

you can just do

class UserController {
  // the transitive dependency on UserModel is hidden
  constructor(private userService: UserService) {}
}

Class Reference

But for the DI to know which service to inject you need some link from the @Inject declaration to an actual class to instantiate. Of course, this mechanism depends on the implementation of the DI system. The reference could be by name (string matching), by interface (DI decides which implementation to use: UserService -> UserServiceImpl / MockUserServiceImpl) or in the default case of nestjs directly by the class to be instantiated.

Although matching by name is possible in nestjs, matching by class is preferred because it makes refactoring much easier.

When you create a custom provider you can choose what kind of token you want to use for the matching. This is needed, when you want to inject a value (no class for matching)

const connectionProvider = {
  provide: 'Connection',
  useValue: connection,
};

@Module({
  providers: [connectionProvider],
})

or a dynamically instantiated class.

const configServiceProvider = {
  provide: ConfigService,
  useClass: process.env.NODE_ENV === 'development'
    ? DevelopmentConfigService
    : ProductionConfigService,
};

@Module({
  providers: [configServiceProvider],
})

这篇关于在NestJS中导入具有依赖注入的TypeScript模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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