Skip to content

Entity (DDD)

Spicy — senior dev territoryBackend

ELI5 — The Vibe Check

An entity is an object with a unique identity that persists over time. Even if a User changes their name, email, and address, they're still the same User because the ID stays the same. It's like you — you've changed every cell in your body since childhood, but you're still you. Identity matters more than attributes.

Real Talk

In Domain-Driven Design, an entity is a domain object defined by its identity rather than its attributes. Two entities with the same attributes but different IDs are distinct objects. Entities have a lifecycle, can be mutated, and are typically persisted in databases. They encapsulate business rules and maintain invariants about their state.

Show Me The Code

class User {
  constructor(
    public readonly id: string,  // Identity
    private name: string,
    private email: string
  ) {}
  changeName(newName: string): void {
    if (!newName.trim()) throw new Error('Name required');
    this.name = newName;
  }
  equals(other: User): boolean {
    return this.id === other.id;  // Identity-based equality
  }
}

When You'll Hear This

"User is an entity — its identity persists even when attributes change." / "Entities are compared by ID, value objects by their attributes."

Made with passive-aggressive love by manoga.digital. Powered by Claude.