我如何从控制器动态调用服务的方法(从哈希映射中查找):
registration.service.ts
import { Injectable } from '@nestjs/common';
import { CacheService } from '../cache/cache.service';
import { LoggerService } from '../../config/logger/logger.service';
@Injectable()
export class RegistrationService {
constructor(private readonly cache: CacheService,
private readonly logger: LoggerService) {
}
async process() {
this.logger.info(`RegistrationService - testThis - hit!`);
}
}
app.controller.ts
import { Controller, Get, Res } from '@nestjs/common';
import { RegistrationService } from './modules/campaignOne/registration.service';
const SCREENS = {
'start': 'this.registration.process'
};
@Controller()
export class AppController {
constructor(private readonly registration: RegistrationService) {}
@Get('test')
async test(@Res() res: any): any {
await eval(SCREENS['start'])();
return Promise.resolve('ok');
}
}
执行await eval(SCREENS['start'])();
时,该方法被调用,但由于this.logger..
无法解析记录器,因此会引发错误。
这个想法是要有多个提供程序,并且控制器中的逻辑将决定动态调用哪个提供程序。
我知道问题和解决方案在于依赖注入和IOC-但我不知道如何解决。
请帮助?
答案 0 :(得分:1)
我将对此使用工厂方法。
首先创建一个界面,以定义所需的“动态”服务的形状
//IService.ts
export interface IService {
invoke(): Promise<void>;
}
现在,要动态化的所有服务都可以实现此接口。 注意:此服务未使用嵌套@Injectable批注,因为该类不是实际将要注入的类,如下所示。
//registration.service.ts
import { CacheService } from '../cache/cache.service';
import { LoggerService } from '../../config/logger/logger.service';
export class RegistrationService implements IService {
private readonly cache: CacheService;
private readonly logger: LoggerService;
constructor(cache: CacheService, logger: LoggerService) {
this.cache = cache;
this.logger = logger;
}
async invoke() : Promise<void> {
this.logger.info(`RegistrationService - testThis - hit!`);
return Promise.resolve();
}
}
现在创建您的工厂类,它将成为IOC注入的东西
//ScreenFactory.ts
import { Injectable } from '@nestjs/common';
import { RegistrationService } from './modules/campaignOne/registration.service';
@Injectable()
export class ScreenFactory {
private readonly cache: CacheService;
private readonly logger: LoggerService;
constructor(cache: CacheService, logger: LoggerService) {
this.cache = cache;
this.logger = logger;
}
public getService(screenName: string) : IService {
switch(screenName) {
case 'start': return new RegistrationService(this.cache, this.logger);
default: throw new Error('No service defined for the given screen');
}
}
}
现在在您的模块中,注入工厂代替
import { Controller, Get, Res } from '@nestjs/common';
import { ScreenFactory } from './modules/ScreenFactory';
@Controller()
export class AppController {
constructor(private readonly screenFactory: ScreenFactory) {}
@Get('test')
async test(@Res() res: any): any {
//await eval(SCREENS['start'])();
const service: IService = this.screenFactory.getService('start');
await service.invoke();
return Promise.resolve('ok');
}
}
这是很多伪代码,但这是我通常处理“每个请求我需要不同的东西”类型的方案的方式。