Generating identities for entities in DDD

后端 未结 2 1041
自闭症患者
自闭症患者 2021-01-02 10:11

Edit

To further clarify my initial problem, I rewrote the question with more \'DDD\'-termini, common patterns and discussion arguments. The orginal version can be

相关标签:
2条回答
  • 2021-01-02 10:42

    I would place it in a factory. Generating id shouldn't be a part of domain logic in my opinion, because it's really an infrastructure matter. You can take id from DB or generate it with uuid or whatever. It's a detail. Also remember that only interface of a factory belongs to domain layer, not its implementation.

    About your doubts for factory, if you use factory to create entities then you should use it everywhere. This is how I do it.

    0 讨论(0)
  • 2021-01-02 11:08

    Vaughn Vernon author of Implementing Domain Driven Design advocates creating unique ids in repositories like this:

       public TenantId nextIdentity() {
            return new TenantId(UUID.randomUUID().toString().toUpperCase());
        }
    

    TenantId is a value object that wraps the identity of the Entity.

    0 讨论(0)
提交回复
热议问题