Command Handler

In CQRS, a command handler is responsible for handling incoming commands and executing the corresponding business logic. It receives a command and performs the necessary operations to update the system’s state.

Here is an example of a command handler in Java:

public class CreateOrderCommandHandler implements CommandHandler<CreateOrderCommand> {
    private final OrderRepository orderRepository;

    public CreateOrderCommandHandler(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    @Override
    public void handle(CreateOrderCommand command) {
        // create an order entity from the command data
        Order order = new Order(command.getCustomerId(), command.getOrderItems());

        // save the order in the repository
        orderRepository.save(order);
    }
}


In this example, the CreateOrderCommandHandler is responsible for handling the CreateOrderCommand. It receives the command and creates a new Order entity based on the command data. It then saves the order in the OrderRepository.

The CommandHandler interface is a generic interface that defines the handle method. The type parameter <CreateOrderCommand> specifies the type of the command that the handler can handle.

The OrderRepository is a dependency of the command handler, which is injected through the constructor. The repository is responsible for persisting and retrieving order entities.

Use with event sourcing

When used in conjunction with event sourcing, command handling involves receiving and processing commands, updating the state of an aggregate by generating events, and persisting those events to an event store. The events are then published to an event bus for consumption by event handlers.

The command handler receives a command, validates it, and loads the appropriate aggregate from the event store. The aggregate then processes the command, generating one or more events that describe the changes made to its state. These events are then persisted to the event store, and published to the event bus.

Event handlers that are subscribed to the relevant event types can then perform their own operations, such as updating read models or invoking downstream services.

Here is an example of a command handler that works with an event-sourced aggregate in Java:

public class OrderCommandHandler {

  private final EventStore eventStore;

  public OrderCommandHandler(EventStore eventStore) {
    this.eventStore = eventStore;
  }

  public void handle(PlaceOrderCommand command) {
    OrderId orderId = command.getOrderId();
    Order order = eventStore.get(Order.class, orderId);

    order.place(command.getCustomerName(), command.getOrderItems());

    eventStore.save(order.getUncommittedChanges());
  }
}

In this example, the OrderCommandHandler receives a PlaceOrderCommand and loads the corresponding Order aggregate from the event store using its ID. The place method of the Order aggregate then processes the command, generating an event that describes the order being placed. The uncommitted changes of the aggregate are then saved to the event store, which persists the new event.