Nest无法解析ICommandBusAdapter的依赖项

时间:2019-03-27 14:59:56

标签: 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 {}

AdapterModule.ts

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

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

@Module({
  providers: [...providers],
  exports: [...providers],
})
export class AdapterModule {}

UserModule.ts

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

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);
  };
}

CreateUserAction.ts

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

export class CreateUserAction {
  constructor(
    @Inject('ICommandBusAdapter')
    private readonly commandBus: ICommandBusAdapter,
  ) {}
// ...

1 个答案:

答案 0 :(得分:0)

您还记得将CqrsModule添加到您的应用程序吗?

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

@Module({
  imports: [CqrsModule]
  ....

没有它,将无法提供您要注入的CommandBus。

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