Package org.seedstack.business.domain
Interface Entity<I>
-
- Type Parameters:
I- the type of the entity identifier.
- All Known Subinterfaces:
AggregateRoot<I>
@DomainEntity public interface Entity<I>
An entity represent a thread of continuity and identity, going through a lifecycle, though its attributes may change. It is not defined primarily by its attributes but by its identity that stays the same through time and across distinct representations.The identity of an entity must be unique and immutable. It must be chosen carefully and well defined in the model. Identification can come from:
- The outside: a user of the system can provide the identity, handling the uniqueness himself.
- The inside: the entity can generate its own identity using an algorithm.
- An
IdentityGenerator.
An entity should not be merely a holder of attributes, but should also contain the behavior that is directly relevant to it. Do not create entities with only getters and setters but add methods with meaningful business names, implementing domain behavior.
The
BaseEntityclass can be used as a base class for domain entities. It provides an implementation of thegetId(),equals(Object)andhashCode()methods.Example:
public class SomeEntity implements Entity<SomeEntityId> { private SomeEntityId id; public SomeEntity(SomeEntityId id) { this.id = id; } @Override public SomeEntityId getId() { return this.id; } @Override public int hashCode() { // implement using identity attribute only } @Override public boolean equals() { // implement using identity attribute only } // Other methods }
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanequals(Object other)As per Domain-Driven Design semantics, entity equality must be computed on its identity only, as returned by thegetId()method.IgetId()Returns the identifier of this entity instance.inthashCode()As per Domain-Driven Design semantics, the hash code of an entity must be computed on its identity only, as return by as returned by thegetId()method.
-
-
-
Method Detail
-
getId
I getId()
Returns the identifier of this entity instance.- Returns:
- the entity identifier.
-
equals
boolean equals(Object other)
As per Domain-Driven Design semantics, entity equality must be computed on its identity only, as returned by thegetId()method. Two entities from the same class hierarchy (with an inheritance relationship between them) can be checked for equality as they have a comparable identity.When implementing this method for entities, the semantics above must be respected in addition to the semantics of
Object.equals(Object).
-
-