Domain Entity
Goal
Define a domain entity as a domain object with a unique identity that persists over time, even as its attributes change.
A domain entity encapsulates business behavior, business rules, and invariants related to its own data. It acts as a core building block in the domain or business layer for concepts such as a customer, order, subscription, account, or shipment.
Treat a domain entity as identity-driven code. The key question is whether the object represents a specific domain concept that remains the same thing throughout its lifecycle while its state evolves.
What Counts as a Domain Entity
Classify code as a domain entity when it does one or more of these things:
- represents a specific domain concept through a stable identity
- carries an identifier that distinguishes one instance from another across time
- preserves continuity as state changes throughout a lifecycle
- exposes behaviors that apply domain rules to its own data
- enforces invariants that must remain true for that specific domain concept
- controls valid state transitions for its own lifecycle
- encapsulates internal state and requires callers to use intention-revealing methods
- protects its data from arbitrary mutation so validity is maintained
Domain entities often appear in code that answers questions such as:
- which specific customer, order, account, or shipment is this
- what makes this object the same domain thing after its attributes change
- which operations may change its state
- which conditions must remain true throughout its lifecycle
- how callers must interact with it to keep it valid
Detection Workflow
Read the code for identity first.
- Look for identifiers, natural keys, references, or equality rules that distinguish one instance from another.
- Pay attention to code that tracks the same domain concept over time rather than only its current attributes.
Identify lifecycle continuity.
- Determine whether the object remains the same domain concept as its properties change.
- Identify the states, transitions, and milestones that define its lifecycle.
Trace behaviors and invariants.
- Identify methods that change the entity's state, enforce rules, or reject invalid transitions.
- Identify the conditions that must always hold true for the entity to remain valid.
Check the encapsulation boundary.
- Identify whether callers are expected to change state through explicit methods instead of direct arbitrary mutation.
- Prefer designs where domain operations are expressed as named behaviors with domain meaning.
Prefer semantic classification to file or framework conventions.
- Do not assume code is or is not a domain entity only because of its folder, class name, annotation, ORM mapping, or framework role.
- Classify by whether the object models a stable domain identity with owned behavior and invariants.
Writing or Changing Domain Entities
Preserve the identity model before refactoring.
- Restate what makes the entity the same domain concept over time.
- Keep identifiers and identity semantics explicit.
Use a class to co-locate identity, state, invariants, and behavior.
- Use a class, or the closest class-like construct available in the language, so that identity, state, invariants, and domain behavior live together in a single construct.
- A plain type alias, interface, or record paired with standalone functions is not a class-like construct. Do not model a domain entity that way.
- Follow the project's existing conventions for how classes are modeled.
- Only fall back to a non-class construct when the language has no class support at all, not merely because a functional style is common or preferred by convention.
Keep behavior close to the entity.
- Put state-changing domain operations on the entity when they belong to that entity's own rules.
- Use method names that express business intent, such as
changeAddress, approve, cancel, or assignOwner.
Protect invariants through explicit operations.
- Verify that every allowed state change still enforces the required rules.
- Avoid exposing write paths that let callers bypass validity checks.
Preserve lifecycle integrity.
- Verify that transitions between states remain valid and understandable.
- Verify that the entity keeps the same identity across creation, loading, updates, and later lifecycle stages.
Hide internal structure where validity depends on it.
- Expose the minimum data and methods needed for callers to interact correctly.
- Prefer intention-revealing methods over unrestricted setters when rules must be enforced.
Review Questions
When reading or reviewing code, ask:
- What makes this object the same domain thing over time?
- Which identifier distinguishes it from other instances?
- Which behaviors belong to this object as part of its own domain responsibility?
- Which invariants must remain true for it to stay valid?
- Would changing this code alter the identity, lifecycle, or rule enforcement of a specific domain concept?
If the answer is yes, treat the code as a domain entity.
Report the Outcome
When finishing the task:
- state which code was identified or treated as a domain entity
- state whether a class or equivalent construct was used, and why
- state which identities, behaviors, lifecycle transitions, or invariants were implemented or preserved
- state which methods or rules protect the entity's validity
1---2name: domain-entity3description: Identify, interpret, review, or write domain entities in code. Use when an agent needs to decide whether a domain entity is defined by a stable identity that persists over time, or when it must implement, preserve, or refactor entity lifecycle, invariants, and behavior around domain data. When writing or changing a domain entity, use a class or the closest class-like construct the project stack supports. Do not model domain entities as plain type aliases or interfaces paired with standalone functions.4---56# Domain Entity78## Goal910Define a domain entity as a domain object with a unique identity that persists over time, even as its attributes change.1112A domain entity encapsulates business behavior, business rules, and invariants related to its own data. It acts as a core building block in the domain or business layer for concepts such as a customer, order, subscription, account, or shipment.1314Treat a domain entity as identity-driven code. The key question is whether the object represents a specific domain concept that remains the same thing throughout its lifecycle while its state evolves.1516## What Counts as a Domain Entity1718Classify code as a domain entity when it does one or more of these things:1920- represents a specific domain concept through a stable identity21- carries an identifier that distinguishes one instance from another across time22- preserves continuity as state changes throughout a lifecycle23- exposes behaviors that apply domain rules to its own data24- enforces invariants that must remain true for that specific domain concept25- controls valid state transitions for its own lifecycle26- encapsulates internal state and requires callers to use intention-revealing methods27- protects its data from arbitrary mutation so validity is maintained2829Domain entities often appear in code that answers questions such as:3031- which specific customer, order, account, or shipment is this32- what makes this object the same domain thing after its attributes change33- which operations may change its state34- which conditions must remain true throughout its lifecycle35- how callers must interact with it to keep it valid3637## Detection Workflow38391. Read the code for identity first.40 - Look for identifiers, natural keys, references, or equality rules that distinguish one instance from another.41 - Pay attention to code that tracks the same domain concept over time rather than only its current attributes.42432. Identify lifecycle continuity.44 - Determine whether the object remains the same domain concept as its properties change.45 - Identify the states, transitions, and milestones that define its lifecycle.46473. Trace behaviors and invariants.48 - Identify methods that change the entity's state, enforce rules, or reject invalid transitions.49 - Identify the conditions that must always hold true for the entity to remain valid.50514. Check the encapsulation boundary.52 - Identify whether callers are expected to change state through explicit methods instead of direct arbitrary mutation.53 - Prefer designs where domain operations are expressed as named behaviors with domain meaning.54555. Prefer semantic classification to file or framework conventions.56 - Do not assume code is or is not a domain entity only because of its folder, class name, annotation, ORM mapping, or framework role.57 - Classify by whether the object models a stable domain identity with owned behavior and invariants.5859## Writing or Changing Domain Entities60611. Preserve the identity model before refactoring.62 - Restate what makes the entity the same domain concept over time.63 - Keep identifiers and identity semantics explicit.64652. Use a class to co-locate identity, state, invariants, and behavior.66 - Use a class, or the closest class-like construct available in the language, so that identity, state, invariants, and domain behavior live together in a single construct.67 - A plain type alias, interface, or record paired with standalone functions is not a class-like construct. Do not model a domain entity that way.68 - Follow the project's existing conventions for how classes are modeled.69 - Only fall back to a non-class construct when the language has no class support at all, not merely because a functional style is common or preferred by convention.70713. Keep behavior close to the entity.72 - Put state-changing domain operations on the entity when they belong to that entity's own rules.73 - Use method names that express business intent, such as `changeAddress`, `approve`, `cancel`, or `assignOwner`.74754. Protect invariants through explicit operations.76 - Verify that every allowed state change still enforces the required rules.77 - Avoid exposing write paths that let callers bypass validity checks.78795. Preserve lifecycle integrity.80 - Verify that transitions between states remain valid and understandable.81 - Verify that the entity keeps the same identity across creation, loading, updates, and later lifecycle stages.82836. Hide internal structure where validity depends on it.84 - Expose the minimum data and methods needed for callers to interact correctly.85 - Prefer intention-revealing methods over unrestricted setters when rules must be enforced.8687## Review Questions8889When reading or reviewing code, ask:9091- What makes this object the same domain thing over time?92- Which identifier distinguishes it from other instances?93- Which behaviors belong to this object as part of its own domain responsibility?94- Which invariants must remain true for it to stay valid?95- Would changing this code alter the identity, lifecycle, or rule enforcement of a specific domain concept?9697If the answer is yes, treat the code as a domain entity.9899## Report the Outcome100101When finishing the task:102103- state which code was identified or treated as a domain entity104- state whether a class or equivalent construct was used, and why105- state which identities, behaviors, lifecycle transitions, or invariants were implemented or preserved106- state which methods or rules protect the entity's validity