Entity Design Skill

Design domain entities with encapsulation, validation, and invariant checks.

1|Updated Sep 22, 2025
One-click install
npx skills add https://github.com/pretodev/nice_app --skill entity-design-skill
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Entity Design Skill
Source: https://github.com/pretodev/nice_app/tree/main/.trae/skills/entity-design-skill
Command: npx skills add https://github.com/pretodev/nice_app --skill entity-design-skill

SYSTEM DOCUMENTATION & REQUIREMENTS

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.

Frequently Asked Questions about Entity Design Skill

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I enforce domain invariants and encapsulation in DDD entities?

Domain invariants and encapsulation are enforced by using private fields with unmodifiable getters and overriding a validation hook to throw domain-specific failures. State mutations occur only through guarded domain methods that always validate business rules and update timestamps.

What is the best way to create and validate domain entities?

The best way to create and validate domain entities is through a dedicated creation factory, such as a .create() method. This factory initializes the entity in a valid state, sets required timestamps like createdAt and updatedAt, and ensures the entity is active before any subsequent state changes occur.

How do I reconstitute domain entities from persistence layers?

To reconstitute domain entities from persistence layers, you expose a public constructor accepting all entity fields. This allows persistence rehydration while bypassing the creation factory, letting you rebuild exact stored states without triggering initial timestamp generation or default active status rules.

Why does my domain entity require a specialized failure type?

A domain entity requires a specialized failure type to accurately report domain-specific invariant violations. Creating a dedicated failure subclass distinguishes business rule breaches from standard system errors, making it easier to catch and handle specific domain validation failures across services.

Can I use GuidEntity or SerialEntity for domain entity identity handling?

Yes, you can use GuidEntity, SerialEntity, or a generic Entity<T> for domain entity identity handling. Choosing the appropriate identity type allows you to match the specific creation context of your application, whether you require globally unique identifiers or sequential database-driven keys.

What are the limitations of mutating entity state through domain methods?

The limitation of mutating entity state through domain methods is that every state change must strictly update the updatedAt timestamp and validate business rules. This ensures robust invariant checks but prevents direct field modification, requiring all mutations to pass through guarded domain methods.