Skip to main content
Glama
entity.ts1.71 kB
/** * @fileoverview Base entity class for domain-driven design * * This module provides the base class for all entities in the domain layer. * Entities are domain objects that have a unique identity that persists * throughout their lifecycle, even as their attributes change. */ /** * Base class for all entities in the domain * * Entities are distinguished by their identity rather than their attributes. * Two entities with the same attributes but different identities are considered different. * * @example * ```typescript * class User extends Entity<string> { * constructor( * id: string, * private _name: string, * private _email: string * ) { * super(id); * } * * get name(): string { * return this._name; * } * * changeName(name: string): void { * if (!name || name.length === 0) { * throw new Error('Name cannot be empty'); * } * this._name = name; * } * } * ``` */ export abstract class Entity<TId> { protected readonly _id: TId; protected constructor(id: TId) { this._id = id; } /** * Gets the unique identifier of the entity */ get id(): TId { return this._id; } /** * Checks equality between two entities * * Two entities are equal if they have the same identity, * regardless of their other attributes. * * @param other - The entity to compare with * @returns True if the entities have the same identity, false otherwise */ public equals(other: Entity<TId>): boolean { if (other === null || other === undefined) { return false; } if (!(other instanceof Entity)) { return false; } return this._id === other._id; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sapientpants/deepsource-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server