Auto-Incrementing Ids assigned from repository #180
-
First of all, thank you for this repository. I reference it often. My question is around auto-incrementing IDs assigned by the DB, and it's affect on the design of aggregates, and their domain events. For instead, if an aggregate is created, the The alternative I can think of, is to instead have a Domain Service with a The other option I can think of is to still have a random UUID for the id, and a separate id assigned by the system, but it seems like a hefty tradeoff to introduce two mechanisms for determining uniqueness of an entity. Any insight would be greatly appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Auto-incrementing IDs for aggregates have some disadvantages:
It's a common practice to use 2 ids in databases, a primary key and a surrogate key, so you can have both. If you still want to stick to auto-incrementing ids, just change the code accordingly to support it (maybe publish events after aggregate is saved and id is issued). |
Beta Was this translation helpful? Give feedback.
The solution I came up with (I'm working in java, so I'm only taking inspiration here) is to modify the AggregateRoot's
publishEvents
method to also take an aggregate Id argument. Each of the domain events are extended from a DomainEventWithAutoId base class that has anissueId
method that allows a caller to mutate the domain event's Id (I could make it immutable with more work). So, thecreate
method stores a domain event where the id is null. Then, when the repository saves the record and gets an id back, it issues it to the aggregate viaaggregate.publishEvents(eventPublisher, savedAggregateId)
. Within that method, it iterates over all the domain events it has locally and callsevent.i…