Provided that I have 2 aggregate, order and customer, and a OrderPayCommand, in the handler, it will check the order status, and check customer balance. If all succeed, it will send an event OrderPaidEvent, which will be handled by Customer aggregate to update balance.
So, how can I check the condition on 2 aggregates in one command handler? So, am I using it in a wrong way?
Within the Axon Framework a Command can only ever be handled by one @CommandHandler
annotated function. Thus, only one Aggregate will be in charge of handling that action.
If you're orchestrating actions between several aggregates, that typically means you can use a Saga
. A Saga in Axon will have associations to several aggregates, which enables you to listen to events from all those aggregates and issue commands to them.
This thus enables you to handle certain events like the OrderPaidEvent
for example, and upon handling that in your Saga you can issue an AdjustCustomerBalanceCommand
to the Customer
aggregate you're associated with.
In short: a command is always handled by just one Aggregate/Entity. For orchestrating actions between aggregates, I'd suggest taking a look at the Saga.