我已经在CQRS recipe之后实现了CQRS模式。 我有这样的命令:
import { ICommand } from '@nestjs/cqrs';
export class PlaceOrderCommand implements ICommand {
constructor(public readonly order: Order) {}
}
还有一个命令处理程序:
import { ICommandHandler, CommandHandler, EventBus } from '@nestjs/cqrs';
@CommandHandler(PlaceOrderCommand)
export class PlaceOrderHandler implements ICommandHandler {
constructor(private eventbus: EventBus) {}
async execute(command: PlaceOrderCommand): Promise<any> {
// Business logic here...
// Publish event to notify case is created.
this.eventbus.publish(orderPlacedEvent);
}
}
现在情况是,我的事件处理程序OrderPlacedEventHandler
在另一个微服务(发票微服务)中。如何确保我的eventHandler侦听此OrderPlacedEvent
?
import { IEventHandler, EventsHandler} from '@nestjs/cqrs';
@EventsHandler(OrderPlacedEvent)
export class orderPlacedEventHandler implements IEventHandler<OrderPlacedEvent> {
constructor(private commandbus: CommandBus) {}
handle(event: OrderPlacedEvent) {
// Business logic goes here...
}
}