Using multiple aggregate in one command handler.

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?

Hi Mavlarn,

I believe I’ve answered you’re question on StackOverflow as well. :wink:

So, let me be a little bit lazy, and repeat my answer here as a reference:

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.

I believe you’ve approved my answer already, so assume this was sufficient.

Cheers,

Steven