In Domain Driven Design, one of the defining characteristic of an Entity is that it has an identity.
Problem:
I am not able to provide a uni
I am not able to provide a unique identity to Entities on instance creation. This identity is only provided by the repository once the entity is persisted (this value is provided from the underlying database).
How many places do you have where you create a list of entities of the same type and you have more then one entity with default id?
Would you consider this to be a good alternative to generating Guid values on instance creation?
If you do not use any ORM your approach is good enough. Especially, when an implementation of identity map and unit of work is your respomsibility. But you have fixed Equals(object obj)
only. GetHashCode()
method does not check if uniqueId.Equals(default(IdType))
.
I suggest you look into any open-source "Infrastructure Boilerplate" like Sharp-Architecture and check their implementation of the base class for all domain entities.
I am used to writing custom implementations of Equals()
for domain entities, but it can be superfluous when it comes to usage of ORM. If you use any ORM it provides implementations of identity map and unit of work patterns out of the box and you can rely on them.