0

I'm currently developing message imap-based module in crm app and i'm trying to connect dots using ddd principals.

I've started with three main entities, all bound under Account Aggregate:

  • Account - Email account. Has multiple Folders.
  • Folder - Folder in email account (ex. Inbox, Sent, Draft). Has multiple Messages.
  • Message - Email message.

In this case invariant is on the actual collections, mostly between Account and Folder:

  • Account can't have two folders with the same name,
  • Account can't have multiple special folders (like Inbox or Sent),
  • Mailbox should have actual message count.

enter image description here

With this solution i ended with breaking rule of creating small aggregates. This also can cause performance issues. Almost every account has around six folders, that can contains thousands of messages. The amount of data loaded to memory. Then I tried to split it to two aggregates Account (with Folder as local entity) and Message. In this case I'm breaking rule of referencing local entity (Folder) to another aggregate (Message), because message is bounded to specific folder.

enter image description here

In the end I tried to split them all to separate aggregates. In that case, no aggregate rules are broken, but I'm breaking invariant between Account and Folder. As a result there is need of adding another layer of complexity using domain services and threat of most likely updating at least two aggregates in one transaction. Also this forces Folder, as it's entity between aggregates to become aggregate root while it never meant be one.

enter image description here

What is the best way to split this entities in this case?

[Edit] I added additional information based on comments.

4
  • Can you explain why your first solution is not a small aggregate. Nov 30, 2021 at 8:51
  • I would argue that folders belong more in the message agg root than the account agg root. Folders only exist as a way to sort messages. Not posting this as an answer bc my knowledge on invariance is rusty and I might be missing a larger issue at play here, not sure if this fixes your issue or is just a tangential remark.
    – Flater
    Nov 30, 2021 at 9:54
  • @BartvanIngenSchenau In most cases account has around six folders, that can contains thousands of messages. The amount of data loaded to memory, that was almost never used forced idea to split Account Aggregate. I took Vernon's approach from his essay - Effective Aggregate Design Part III
    – devmr97
    Nov 30, 2021 at 10:17
  • @Flater That's fair point, yet i want to split Message as aggregate root mostly because of performance issues (thousands of messages inside folder, that need to be loaded to memory). Also folder has so much to to with account: - Account can't have two folders with the same name, - Account can't have multiple special folders (like Inbox or Sent) etc.
    – devmr97
    Nov 30, 2021 at 10:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy