intermediateBuilding the Aggregate — the Write Model

A teammate writes a @CommandHandler that validates a command and then sets this.name = command.getName() directly, without applying any event. What is wrong with this, and what will actually happen at runtime?

This breaks the event sourcing model at its foundation: in an event-sourced aggregate, state is only ever supposed to change as a result of replaying events through @EventSourcingHandler methods, never through a direct field assignment inside a command handler. At runtime, the immediate effect looks fine — the field really is set on that in-memory instance, and any command handled right afterward in the same session sees the updated value. The bug surfaces the next time this aggregate has to be rebuilt from its event history, which happens routinely whenever a fresh instance is loaded to handle a new command: since no event was ever applied for that name change, it isn't part of the aggregate's event history, and the rebuilt instance's name field reverts to whatever it was before the direct assignment, silently losing the change.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Why must an @EventSourcingHandler method never throw an exception for a business-rule reason?← Back to all Event-Driven Microservices questions