mercredi 10 mars 2021

First time using DDD; am I right on what's a Entity/VO and the way I map the domain to persistence?

This is my first app with DDD in mind and now that (I hope) my simple domain is set up, I want to make sure everything's okay before I think about persistence.

type Todo = {
  id: string,
  content: string,
  dateCreated: Date,
  done: boolean
}

type Folder = {
  id: string,
  name: string,
  todos: string[] // an array of Ids, they only hold reference to todos
}

type User = {
  id: string,
  username: Username,
  email: Email,
  password: Password,
  folders: Record<string, Folder>,
  todos: Record<string, Todo>
}

Here are the questions I have:

  1. User is an aggregate root, and Folder and Todo are aggregates of User, correct? If, for example, I need to change the content of an Afazer, I must do so through User, right?
  2. For some reason, Folder seems like a Value Object to me; it just holds a bunch of IDs. But I also feel like it should be an Entity because it has a lifecycle? (A User can delete/create folders).
  3. Can Folder just reference Todos by their IDs? That's no problem, right? Or do I have to map each of these IDs to an actual entity? But then, I'd have duplicates (Folder and User would have the same Todo but in different locations), that feels like a no no

Now, more database-related questions:

  1. Aren't Folders and Todos also aggregate roots? So, they should have their own repositories and their own tables, correct?

  2. This is also my first time using a relational database, coming from NoSQL. If I am correct that Folder is an aggregate root, and thus it should have its own repository, and thus it should have its own table, then how would it reference the User it belongs to? Should the User reference the Folders it has, or should the Folder reference its owner?

  3. Same questions for the Todos, should they reference their owner, or should User reference the todo it has?

So, when I map User to a persistence DTO, it would look something like:

{
  id: string,
  username: string,
  email: string,
  password: string,
  folders: string[] // array of IDs, or again, as I asked, should the folder reference the user instead, or both?
  todos: string[] // same question
}

a Folder would be mapped as such?:

{
  id: string,
  name: string,
  ownerId: string, // ?
  todos: string[]
}

I know these are really simple questions but I want to make sure I'm doing everything right so I can proceed to more complex domains.

Thank you so much

Aucun commentaire:

Enregistrer un commentaire