Many to Many relations between Aggregates #139
-
I'm working on a SaaS project, and I have the following root aggregates: export class Person {} and export class Company {} How can I create a relation between these two aggregates? I know that I must use an object value. In the case of a Person: export class Memberships {
companyId: string;
role: string;
} And in the case of a Company: export class Members {
memberId: string;
role: string;
} These value objects have the same properties, but I'm repeating code. How can I centralize or improve this code? I'm considering this because I'm working with TypeORM, and at the database level, these value objects represent a unique table. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Similar to this discussion: #132 Solution depends on your use case. Do you need to have members in a company? Most likely yes, so keep it there. export class Company {
members: Member[]
} Do you need companies in a Person aggregate? are you using it in some way? if not, there is no need for a relation there. If yes, you can keep a list of company IDs in a export class Person {
// ...
companyIds: string[]
} To get a role you can load a company and get a role from there, instead of keeping role in two places. Personally I would not put companies into a Person aggregate, there is no need for it to know about companies in most cases, you have Company aggregate for keeping that info and doing operations on it. |
Beta Was this translation helpful? Give feedback.
Similar to this discussion: #132
Solution depends on your use case.
Do you need to have members in a company? Most likely yes, so keep it there.
Do you need companies in a Person aggregate? are you using it in some way? if not, there is no need for a relation there. If yes, you can keep a list of company IDs in a
Person
.To get a role you can load a company and get a role from there, instead of keeping role in two places.
Personally I would not put companies into a Person aggregate, there is no need for it to know about companies in most cases, you have Company aggregate for keeping that …