Axon 2 : applying events inside the event sourcing handler

Hi all,

Since Axon 2.4, it’s possible to apply events directly from the event sourcing handler.

I can see the benefit if for example, one event raised by an event sourcing member should raise another event in the Aggregate Root.

But except this case, in case of a Single AR without any entities, what’s the difference between publishing directly several events from the command handler and applying them in a nested way ?

What are the real use cases that motivated this change ?

Thanks,
Regards,
Jerome

Hi Jerome,

not sure what change you’re referring to. The mechanism exists in 2.4 as well as 3.
It is recommended to apply events from the Command handler only. However, in certain situations (like when you have multiple entities in an aggregate), it is more practical to react on an event instead.

There is a small difference in the two approaches: when applying an event from a Command handler, your change is guaranteed to have been applied when the apply() method returns. In case of an Event Handler, it will be applied after the current event handler has completed. To execute logic after the event has been applied, you will need to use apply().andThen(…).

Cheers,

Allard