如何在NestJs中使用CQRS模式编写微服务

时间:2020-01-02 12:39:25

标签: microservices nestjs

我已经在CQRS recipe之后实现了CQRS模式。 我有这样的命令:

src / commands / place-order.command.ts

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

export class PlaceOrderCommand implements ICommand {
    constructor(public readonly order: Order) {}    
}

还有一个命令处理程序:

src / command / place-order.handler.ts

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...
    }
}

0 个答案:

没有答案