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:
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.
If you have better answer, please add a comment about this, thank you!