Providers
Providers是Nest的一个基本概念
许多基本Nest类都可以被视为Provider:service、repository、factory、helper,等等
它们都可以通过constructor注入依赖关系,这意味着对象可以彼此创建各种关系,并且”连接“对象实例的功能在很大程度上可以委托给Nest运行时系统
Provider是一个用@Injectable()装饰器注释的类
个人理解:Provide类似Spring的@Bean?
用法
基本用法
Service中使用了@Injectable:
1 2 3 4 5 6 7 8 9
| import { Injectable } from '@nestjs/common';
@Injectable() export class AppService { getHello(): string { return 'hello!'; } }
|
Module中导入了Service,并将Service注入Providers:
1 2 3 4 5 6 7 8 9 10 11 12
| import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UserModule } from './user/user.module';
@Module({ imports: [UserModule], controllers: [AppController], providers: [AppService], }) export class AppModule {}
|
在Controller中使用注入好的Service:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service';
@Controller('get') export class AppController { constructor(private readonly appService: AppService) {}
@Get('hello') getHello(): string { return this.appService.getHello(); } }
|
自定义名称
注入Providers时给Service起个自定义名称:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UserModule } from './user/user.module';
@Module({ imports: [UserModule], controllers: [AppController], providers: [ { provide: 'omg', useClass: AppService, }, ], }) export class AppModule {}
|
使用@Inject通过自定义名称找到Provider:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import { Controller, Get, Inject } from '@nestjs/common'; import { AppService } from './app.service';
@Controller('get') export class AppController { constructor(@Inject('omg') private readonly appService: AppService) {}
@Get('hello') getHello(): string { return this.appService.getHello(); } }
|
自定义注入值
把一些值注入Providers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UserModule } from './user/user.module';
@Module({ imports: [UserModule], controllers: [AppController], providers: [ AppService, { provide: 'Test', useValue: [1, 1, 4, 5, 1, 4], }, ], }) export class AppModule {}
|
使用@Inject通过名称找到自定义注入值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import { Controller, Get, Inject } from '@nestjs/common'; import { AppService } from './app.service';
@Controller('get') export class AppController { constructor( private readonly appService: AppService, @Inject('Test') private readonly yajue: number[], ) {}
@Get('hello') getHello(): number[] { return this.yajue; } }
|
工厂模式
如果Service间有相互的依赖或逻辑处理,可以使用工厂模式
把一个工厂函数注入Providers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { UserModule } from './user/user.module';
@Module({ imports: [UserModule], controllers: [AppController], providers: [ AppService, { provide: 'CCC', inject: [AppService], useFactory(AppService: AppService) { console.log(AppService.getHello()); return Math.random() < 0.5 ? true : false; }, }, ], }) export class AppModule {}
|
使用@Inject通过名称获取工厂函数的返回值,异步返回值也可以:
1 2 3 4 5 6 7 8 9 10 11 12
| import { Controller, Get, Inject } from '@nestjs/common';
@Controller('get') export class AppController { constructor(@Inject('CCC') private readonly ccc: boolean) {}
@Get('hello') getHello(): boolean { return this.ccc; } }
|