Question

Domain entities are always unique and therefore need an identifier that's unique accross the domain. A common approach is to decorate a domain entity with a id and leave it open to the underlying architectural technology to set that id.

public MyDomainEntity {  
    int Id { get; set; }
}

Mostly, domain entities get stored in a database that is capable of auto incrementing ids. Or we use a ORM mapper that allowes to generate ids (UUIDs etc.)

However, I find this approach very problematic. Who guarantees that the e.g. database is capable of generating an id? And who ensures that the automatic id fits the requirements of the domain?

If I want to specify how entities are being made unique and how new ids are being generated - where do I implement this? On the repository implementations? On UnitOfWorks?

Was it helpful?

Solution

since an id is something technical, there should not be any requirements of the domain towards id generation.

If you want to control id generation, then I think the repository would be the best place to do it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top