• CQRS With Axon


    CQRS With Axon

     
     

    CQRS implementation with Axon[edit]

    RTENOTITLE

    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.

    RTENOTITLE

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

    RTENOTITLE

     
    @Value
    class ApplicationSubmittedEvent {
    private String appId;
    private String category;
    }
     
    @Aggregate
    @NoArgsConstructor
    public class ApplicationAggregate {
    @AggregateIdentifier
    private String id;
     
    @CommandHandler
    public ApplicationAggregate(SubmitApplicationCommand command) {
    //some validation
    this.id = command.getAppId;
    apply(new ApplicationSubmittedEvent(command.getAppId(), command.getCategory()));
    }
    }

    To change the state of the write DB, we need to provide a method annotated with @EventHandler.

    The application can contain multiple event handlers. Each of them should perform one specific task like sending emails, logging or saving in database.

    RTENOTITLE

     
    @RequiredArgsConstructor
    @Order(1)
    public class ProjectingEventHandler {
    private final IApplicationSubmittedProjection projection;
     
    @EventHandler
    public CompletableFuture<Void> onApplicationSubmitted(ExpertsFormSubmittedEvent event) {
    return projection.submitApplication(event.getApplicationId(), event.getCategory());
    }
    }

    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.

    Conclusion[edit]

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

     

     

  • 相关阅读:
    android adb
    5 个免费的受欢迎的 SQLite 管理工具
    [Android]通过setImageURI设置网络上面的图片
    Android TextView实现长按复制文本功能的方法
    View工作原理(四)view的layout过程
    Anaroid WebView详解大全
    Android 如何在Eclipse中查看Android API源码以及support包源码
    关于Android的.so文件你所需要知道的
    AS问题解决系列3—iCCP: Not recognizing known sRGB profile(转)
    安卓App设计博文
  • 原文地址:https://www.cnblogs.com/lshan/p/11425706.html
Copyright © 2020-2023  润新知