1

像 AXON 这样的 CQRS 模式在聚合中使用 apply 方法,最终将事件发布到事件总线,命令处理程序也可以访问事件总线以将 commandHandled 事件发布到事件总线。

有什么优点和缺点,什么时候使用什么?

4

1 回答 1

1

聚合中的 Apply 方法通常涉及在事件存储中持久化事件,以及在采用事件溯源时发布事件。

另一方面,当命令可能发出不同类型的事件时,直接在命令处理程序中发布事件通常会强制您的聚合公开更多细节。例如:

//in command handler
public void handle(FooCommand command) {
    Foo aggregate = //retrieve aggregate
    aggregate.handle(command)

    if (aggregate.isFoo()) {
        eventBus.publish(aFooEvent)
    } else if (aggregate.isBar()) {
        eventBus.publish(aBarEvent)
    }
}
于 2014-12-18T08:37:01.513 回答