What problem does it solve?
Domain-driven design requires robust, encapsulated domain entities that enforce invariants, validate state, and manage identity, to prevent leakage of business rules into higher layers.
Core Features & Use Cases
- Identity handling: Choose an appropriate identity type (GuidEntity, SerialEntity, or Entity<T>) to match creation context.
- Public constructor for reconstitution: Expose a constructor with all fields to enable persistence rehydration.
- Creation factory: Provide a factory (e.g., .create()) that initializes the entity in a valid state with createdAt/updatedAt timestamps and isActive = true.
- Encapsulation and internal state: Use private fields with getters returning unmodifiable views.
- Domain methods for state changes: Mutate state through domain methods, always updating updatedAt and validating business rules.
- Validation hook: Override validate() to enforce invariants and throw domain-specific failures.
- Specialized failure types: Create a dedicated failure subclass to report domain violations.
- Debugging aids: Override props to expose key domain attributes for easier debugging.
Quick Start
Create a new domain entity by choosing an identity type, implement a public constructor with required fields, and add a creation factory to initialize a valid entity.