CQRS 模式(如 AXON(在聚合中使用 apply 方法,该方法最终将事件发布到事件总线,命令处理程序还可以访问 eventbus 以将 commandHandle 事件发布到事件总线。
有什么优缺点,什么时候使用什么?
在采用事件溯源时,聚合中的应用方法通常涉及在事件存储中保留事件以及事件发布。
另一方面,当命令可能发出不同类型的事件时,直接在命令处理程序中发布事件通常会强制聚合公开更多详细信息。例如:
//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)
}
}