Domain Model

Web of interconnected objects incorporating both behavior and data

A domain model is a rich object, often called an Aggregate in Domain-Driven Design, usually composed of multiple nested objects, connected as an indivisible whole.

The Domain Model contains every operation related to these sub-objects and acts as a Facade. Every operation must go through the Domain Model.

type State = {
  id: string;
  creditPoints: number;
  schedule: Schedule;
};

type Snapshot = {
  id: string;
  creditPoints: number;
  schedule: GetSnapshot<Schedule>;
};

export class Student extends Entity<State, Snapshot> {
  consumeCreditPoints(points: number) {
    this._state.creditPoints -= points;
  }

  hasEnoughCreditPoints(points: number) {
    return this._state.creditPoints >= points;
  }

  isAvailable(dateRange: DateRange) {
    return !this._state.schedule.isBusy(dateRange);
  }

  addLessonToSchedule(lesson: Lesson) {
    this._state.schedule.add(
      new LessonEvent({
        id: lesson.getId(),
        at: lesson.getAt(),
      }),
    );
  }

  snapshot(): Snapshot {
    return {
      id: this._state.id,
      creditPoints: this._state.creditPoints,
      schedule: this._state.schedule.snapshot(),
    };
  }
}

Source : https://martinfowler.com/eaaCatalog/domainModel.html

Last updated