One thing I learned today — 8 January 2018

Niel de Wet
1 min readJan 8, 2018

--

One of the most valuable things I did today was a code review of an Axon aggregate with a fellow colleague. The Axon Framework is a Java CQRS and Event Sourcing framework.

When coding an aggregate it is important to perform all processing in the @CommandHandler before applying state changes only in an @EventSourcingHandler. Not following this rule has serious implications:

  • Performing any processing in an @EventSourcingHandler can lead to a different aggregate state when loading the aggregate from the repository, especially if there are some integration points involved, because it reapplies the event-sourced events.
  • Performing state changes in the @CommandHandler is illegal because commands are not reapplied when aggregates are loaded.

There are also other benefits to following this rule:

  • The result of the command can be returned to where CommandGateway.sendAndWait() was called.
  • Exceptions during processing can be caught and error events can be published before rethrowing those exceptions.

By refactoring according to this rule the design of the aggregate is greatly improved.

--

--

No responses yet