Question Details

No question body available.

Tags

domain-driven-design aggregate invariants

Answers (2)

Accepted Answer Available
Accepted Answer
April 11, 2025 Score: 5 Rep: 85,992 Quality: Expert Completeness: 60%

The problem with your current design is the AR User contains Organisations which belong to other ARs. So its not a Root.

If you defined a Root which contained all the information required to enforce your invariants it would contain all users and all organisations. Because you have defined the invariants such that there are no splits to make smaller AGs.

This might be fine if you have a lot of memory, or the total size is small, but its likely you will have to accept that these invariants are not technically enforceable (in memory, obvs you can do a DB check if the DB contains all the data) and instead have two AGs User, with a list of external keys of organisations, and Organisation, with a list of external keys to Users.

I would then make a Domain Service, with access the the repositories for each to implement the add and remove functionality. It can then check the repo/db and throw appropriate exceptions if an action would violate a constraint.

April 11, 2025 Score: 6 Rep: 47,363 Quality: High Completeness: 100%

I think the issue is that the domain model has a delete function. Deleting records from a database isn't really core business logic in the sense that you are modeling a business process. Instead, treat this as straight-forward CRUD logic in a repository or domain service.

Deleting stuff from the database is not part of the core business domain. It is an infrastructure concern if this translates to physical deletes from a database. If your system is performing a soft delete, then putting this in a domain model could make sense, although I would argue that a soft (or logical) delete is not a delete. Ask your stakeholders what that really means to them. It could mean "Close", "Invalidate", or "Archive". The notion of soft delete is not something I see mentioned in your question, though.

In short:

  • For physical deletes: implement this in a repository or domain service. It isn't business logic.
  • For soft or logical deletes: clarify what this means from a business perspective and don't call the function "delete"; give it a name that is meaningful to the problem domain.

Only after correcting where things should be deleted would I reconsider the other operations on Organization and User.