Nest无法解析ICommandBusAdapter的依赖关系

问题描述 投票:1回答:1

试图在我的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,
  ) {}
// ...
nestjs
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.