According to the diagram above, creating commands, passing them to command bus and then creating events and placing them on event bus is not CQRS yet.
We have to remember about changing the state of write repository and reading current state from the read database. This is the crucial point of the CQRS pattern.
Configuring this flow should be easy as well. While passing the command to the command gateway, Spring searches methods annotated with @CommandHandler with command type as argument.
@Value
class SubmitApplicationCommand {
private String appId;
private String category;
}
@AllArgsConstructor
public class ApplicationService {
private final CommandGateway commandGateway;
public CompletableFuture<Void> createForm(String appId) {
return CompletableFuture.supplyAsync(() -> new SubmitExpertsFormCommand(appId, "Android"))
.thenCompose(commandGateway::send);
}
}
Command handler is responsible, among other things, for sending the created event to the event bus.
It places an event object to statically imported apply() method from AggregateLifecycle. The event is later dispatched to find expected handlers and thanks to configured event store, all events are saved in DB automatically.
@Value
class ApplicationSubmittedEvent {
private String appId;
private String category;
}
@Aggregate
@NoArgsConstructor
public class ApplicationAggregate {
@AggregateIdentifier
private String id;
@CommandHandler
public ApplicationAggregate(SubmitApplicationCommand command) {
If we want to determine the processing order of all event handlers,
we can annotate a class with @Order and set a sequence number. submitApplication() method is responsible for making all the necessary changes and saving new data in write DB.
These are all vital points to make our app event sourced with CQRS pattern principles. Of course, these principles can be applied only in some parts of our application depending on business needs.
Event sourcing is not suitable for every application or module we are building. It is also worth to be cautious while implementing this pattern because a more complex application can be hard to maintain.
Implementation of CQRS and Event Sourcing is straightforward with Axon framework. More details about advanced configuration can be found on Axon’s website https://docs.axonframework.org/.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2018-08-28 controller 允许跨域访问
2018-08-28 Sublime Text 3 安装+注册+汉化
2018-08-28 POI 3.17
2018-08-28 JDK8时间格式转换