ddd: aggregrate root, generalization

Hello in my relational model i have an inheritance I have 3 types of users on the system, but for simplicity I'm showing only 2 and these 3 types have attributes in common and so I thought of creating a person class and applying a generalization to the students, teachers tables

enter image description here

And then modeling my domain, I was left with some doubts regarding how to represent my domain. I thought about creating an IPerson interface and implementing it in my entities: Student and Teacher, and then the first question arises: Are Teacher and Student aggregate root? And in relation to my user table, in this context can it be part of my Student aggregate and my Teacher aggregate, or is my User entity an aggregate?

Person Struct

export type IPerson = {
  user: User
  firstName: string
  lastName: string
  registration: string
}

Commands:

export class CreateTeachCommand implements ICommand {
  constructor(
    public firstName: string,
    public lastName: string,
    public registration: string,
    public salary: number,
    public hourPrice: number,
    public login: string,
    public password: string,
  ) {}
}

export class CreateStudentCommand implements ICommand {
  constructor(
    public firstName: string,
    public lastName: string,
    public registration: string,
    public login: string,
    public password: string,
  ) {}
}

Aggregrates:

export class Teacher implements IPerson {
  private readonly _id: PersonID
  private readonly _firstName: string
  private readonly _lastName: string
  private readonly _registration: string
  private readonly _salary: number
  private readonly _hourPrice: number
  private readonly _user: User

  private constructor(
    firstName: string,
    lastName: string,
    registration: string,
    salary: number,
    hourPrice: number,
    user: User,
    _id?: PersonID,
  ) {
    this._id = _id || new PersonID(v4())
    this._firstName = firstName
    this._lastName = lastName
    this._registration = registration
    this._user = user
    this._hourPrice = hourPrice
    this._salary = salary
  }

  public static CreateNew(cmd: CreateTeachCommand) {
    const account = User.CreateNew(cmd.login, cmd.password)
    return new Teacher(
      cmd.firstName,
      cmd.lastName,
      cmd.registration,
      cmd.salary,
      cmd.hourPrice,
      account,
    )
  }

  get firstName() {
    return this._firstName
  }
  get lastName() {
    return this._lastName
  }
  get registration() {
    return this._registration
  }
  get salary() {
    return this._salary
  }
  get hourPrice() {
    return this._hourPrice
  }
  get id() {
    return this._id
  }
  get user() {
    return this._user
  }
}

export class Student implements IPerson {
  private readonly _id: PersonID
  private readonly _firstName: string
  private readonly _lastName: string
  private readonly _registration: string
  private readonly _user: User

  private constructor(
    firstName: string,
    lastName: string,
    registration: string,
    user: User,
    _id?: PersonID,
  ) {
    this._id = _id || new PersonID(v4())
    this._firstName = firstName
    this._lastName = lastName
    this._registration = registration
    this._user = user
  }

  public static CreateNew(cmd: CreateStudentCommand) {
    const account = User.CreateNew(cmd.login, cmd.password)
    return new Student(cmd.firstName, cmd.lastName, cmd.registration, account)
  }

  get user() {
    return this._user
  }
  get firstName() {
    return this._firstName
  }
  get lastName() {
    return this._lastName
  }
  get registration() {
    return this._registration
  }
  get id() {
    return this._id
  }
}

export class User {
  private _login: string
  private _password: string
  private _id: UserID
  private constructor(_login: string, _password: string, _id?: UserID) {
    this._id = _id || new UserID(v4())
    this._login = _login
    this._password = _password
  }
  public static CreateNew(login: string, password: string) {
    return new User(login, password)
  }
}

I did the following in my typescript code, and if anyone can help me with these questions I am very grateful, I am a little confused. i was wondering if i'm having going the right way



Read more here: https://stackoverflow.com/questions/66997009/ddd-aggregrate-root-generalization

Content Attribution

This content was originally published by ming at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: