Pragmatic Objects
  • Pragmatic Objects
  • What's an Object ?
  • Thinking Object
  • Practices
    • The Pragmatic Practices
    • No Getters & Setters
    • Inherit Wisely
    • Wrap Null
    • Wrap Primitives
    • Wrap Collections
    • Expose Snapshots
    • Abandon Composed Names
    • Don't Check Types
    • Minimize Knowledge
    • Immutability
    • Separate Commands & Queries
    • Abandon Statics
    • Invert Dependencies
  • Patterns
    • Domain Model
    • Always Valid
    • Wrapper
    • Command
    • Procedural Object
    • Compute Object
    • Snapshot
    • Value Object
    • Observability
  • Examples
    • Celsiuses / Fahrenheits
Powered by GitBook
On this page
  1. Patterns

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(),
    };
  }
}
PreviousInvert DependenciesNextAlways Valid

Last updated 1 year ago

Source :

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