How to prevent the same aggregate is created twice

Hello,

It’s easy to put a command handler on the constructor of the aggregate to create new instances of your aggregate.

@CommandHandler
public AggregateXXX(CreateCommand command)
{ … }

But how to prevent the same aggregate is created twice (e.g. createcommand with same ID) ?

Kind regards,

Koen

Hi Koen,

when the same Aggregate is created twice, the Event Store (when using event sourcing) will reject the Event, as one already exists with the same Aggregate Identifier and sequence number (the creation event always has sequence 0).
If you don’t use event sourcing, the specific Repository implementation should ensure that the identifier is unique, and inserting a second entry with the same identifier fails.

Kind regards,

Allard Buijze