How can I prevent Asynchronous EventHandler handles events that should be rollbacked

Hi,

this one pops up every once in a while, although there is a slight twist this time. Kyle is right about the Command handlers being the only one that can “reject” the execution. However, if you’re using Event Sourcing, validating that a username is unique (a.k.a Set validation) is a slight bit harder.

Basically, there are four aproaches:

  • Have the command handler execute a query to validate uniqueness. Just accept the very small chance of a duplicate when two users register the same name at appx the same time
  • Slight modified version of the above, where the command handler keeps a “used usernames” table, in which it updates and reads the usernames being used.
  • Detect the duplicates in the Event Handler that updates the query model. When a duplicate is detected, the second account is blocked. In your case, this would mean an email is sent.
  • Use a 2-step process where a Saga confirms an account. You could send the email based on the confirmation.

Hope this helps.
Cheers,

Allard