Nest 无法解析 ICommandBusAdapter 的依赖关系

Mat*_*ois 2 nestjs

尝试在 CreateUserAction.ts 中使用 ICommandBusAdapter.ts,但出现以下错误: [ExceptionHandler] Nest can't resolve dependencies of the ICommandBusAdapter (?). Please make sure that the argument at index [0] is available in the AdapterModule context

我创建了一个AdapterModule将所有提供程序共享给其他模块的模块,但它似乎不起作用。

任何想法 ?

AppModule.ts

import { UserModule } from './User/UserModule';
import { AdapterModule } from './Common/AdapterModule';

@Module({
  imports: [AdapterModule, UserModule, // ...],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)

适配器模块.ts

import { CommandBusAdapter } from 'src/Infrastructure/Adapter/Bus/CommandBusAdapter';

const providers = [
  { provide: 'ICommandBusAdapter', useClass: CommandBusAdapter },
  // ...
];

@Module({
  providers: [...providers],
  exports: [...providers],
})
export class AdapterModule {}
Run Code Online (Sandbox Code Playgroud)

用户模块.ts

import { Module } from '@nestjs/common';
import { CreateUserAction } from 'src/Infrastructure/Action/User/CreateUserAction';
@Module({
  controllers: [CreateUserAction],
})
export class UserModule {}

Run Code Online (Sandbox Code Playgroud)

CommandBusAdapter.ts

import { CommandBus, ICommand } from '@nestjs/cqrs';
import { ICommandBusAdapter } from 'src/Application/Adapter/Bus/ICommandBusAdapter';

@Injectable()
export class CommandBusAdapter implements ICommandBusAdapter {
  constructor(private readonly commandBus: CommandBus) {}

  execute = (command: ICommand) => {
    return this.commandBus.execute(command);
  };
}
Run Code Online (Sandbox Code Playgroud)

创建用户操作.ts

import { ICommandBusAdapter } from 'src/Application/Adapter/Bus/ICommandBusAdapter';

export class CreateUserAction {
  constructor(
    @Inject('ICommandBusAdapter')
    private readonly commandBus: ICommandBusAdapter,
  ) {}
// ...
Run Code Online (Sandbox Code Playgroud)

Jes*_*ter 6

您是否记得将其添加CqrsModule到您的应用程序中?

import { CqrsModule } from '@nestjs/cqrs';

@Module({
  imports: [CqrsModule]
  ....
Run Code Online (Sandbox Code Playgroud)

如果没有它,就不会提供您尝试注入的 CommandBus 的任何内容。

您可以在此处查看示例: https: //github.com/kamilmysliwiec/nest-cqrs-example/blob/master/src/heroes/heroes.module.ts