0

I have two Aggregates called CompanyAggregate and ProfileAggregate and their Aggregate Root are Company and Profile respectively, a Company can have many Profiles. The view responsible for creating Companies has the possibility to create Profiles too at the same time (there is a list and an append button, it is a View requirement), so when the user clicks the save button both Aggregates should be saved. The question is what is the best approach to achieve that? I have a workflow on mind that is like the following:

  1. Call Company's API to create the Company;
  2. Get the created Company's Id;
  3. Call Profiles' API to create each Profile;
  4. Done.

Should those calls be made from the client side or from our API Gateway (API Decomposition opposed to Composition) OR should I use a domain event? In the latter case I would have to pass Profile data to the Company Aggregate just to raise a domain event leading to a boundary violation.

Some will say Profile should go inside Company Aggregate, but that is not the case as Profile can be directly referenced by another Roots.

Ariel Moraes
  • 602
  • 7
  • 15
  • 1
    It seems your UI is CRUD - oriented, while in CQRS it better to be command-oriented. You literally can have "Create Company" button and "Create Profile" button. Also, regarding CompanyID - we generate it and send with create command, so you don't need to query for it. – Roman Eremin Aug 16 '18 at 14:34
  • I could but the view requirement is to only have 1 button to save. – Ariel Moraes Aug 16 '18 at 14:40
  • What I meant when I said Get the Id is actually get the returned Id from the command response. – Ariel Moraes Aug 16 '18 at 14:44

1 Answers1

1

Since you mentioned that it is possible to create profiles but not mandatory, I would make it two separate commands at the CQRS level. Your intuition about passing Profile data to Company is certainly correct. A profile does not look like a byproduct of the company coming into existence.

On the other hand, making it two distinct requests at the client level will probably cause some ordering problems. I would pack the two in a single HTTP request unless you have some way of pre-generating an identifier for the company to put in both requests.

guillaume31
  • 13,738
  • 1
  • 32
  • 51
  • If I pack them in a single HTTP request do you think it is fine to decompose the view data and call their API through my API Gateway? By doing this I would be maintaining cohesion. Just to complicate things a bit, we have a scenario that depending on the type of the selected Profiles a field of Company should not be empty, what is the best way to handle this? – Ariel Moraes Aug 17 '18 at 02:32
  • With that rule, Company becomes an entirely different animal. Is it valid only at creation or also when you change profiles? – guillaume31 Aug 17 '18 at 07:35
  • This rule is applicable for both creating and updating a Company – Ariel Moraes Aug 17 '18 at 21:58