Class Entity<T>Abstract

Base class for Entities in Domain-Driven Design.

Entities are objects that have a distinct identity that runs through time and different representations. They are compared by their identity rather than their attributes.

class User extends Entity<'User'> {
constructor(
id: EntityId<'User'>,
private email: string,
private name: string
) {
super(id, new Date(), new Date());
}

changeName(newName: string): void {
this.name = newName;
this.touch(); // Update the updatedAt timestamp
}
}

Type Parameters

  • T extends string

    Phantom type representing the entity type (e.g., 'User', 'Order')

Hierarchy (View Summary)

Accessors

Constructors

Methods

Accessors

Constructors

Methods

  • Protected

    Updates the updatedAt timestamp to the current time. Should be called whenever the entity's state changes.

    Returns void

  • Compares this entity with another for equality based on identity. Two entities are equal if they have the same ID and are of the same type.

    Parameters

    • other: Entity<T>

      The other entity to compare with

    Returns boolean

    true if the entities are equal, false otherwise