Entity (DDD)
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."
Related Terms
Aggregate Root
An aggregate root is the boss entity that controls a group of related objects. Want to add an item to an order?
Domain-Driven Design (DDD)
DDD says your code should speak the same language as the business.
Repository Pattern
Repository Pattern puts a layer between your business logic and your database, so your business code never writes SQL directly.
Value Object
A value object is defined by its values, not its identity. Two $10 bills are interchangeable — you don't care which specific bill you have.