• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Aggregate roots in DDD

Resolved: Aggregate roots in DDD

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have registration form where user can input username and password and also create company at the same time(or chose one of existing companies). User have multiple companies and company have multiple users. How can I correctly choose User or Company to be my aggregate root(or both of them?).
I set Company as an aggregate root, it is ok in registration process I’m creating company and adding user to the company (company.addUsers(…)). But when I want to update user profile info(such as name, surname), I cannot do it in company aggregate root. I can create UserAggreageRoot and update user there but in this case it would affect to registration process(because in one transaction UserAggregate and CompanyAggregate would be updated, which is wrong).

Answer:

An aggregate can be viewed as an entity that covers a consistency boundary for a particular action.
If Company and User are in the same domain then you could use Company as an aggregate for creating users:
Your command handler can then create the company and call AddUser and then add the company to the unit of work via a repository. You are only changing one aggregate (consistency boundary). When the unit of work is committed your infrastructure will add the company and user within one transaction.
But then if you just want to change user’s name (and there is no need for the Company to know about that) then you can retrieve a User an aggregate.
A domain class can be an aggregate in one context but not in another.
The command handler will retrieve the User from the repository (and add the user to the unit of work) and make the changes to user before committing the unit of work.

If you have better answer, please add a comment about this, thank you!

aggregateroot domain-driven-design repository-pattern
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: PyCharm cannot see my newly compiled .pyc see on import

27/03/2023

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.