0

I have two aggregate roots: employees and company. Using CQRS I have 2 actions to create every model: CreateCompany (/company) and NewEmploy (/employe) by POST. As well, 2 actions to retrieve both GetCompany (/company/{id}) and GetEmploy (/employe/{1}) by GET. I want to get the employees that belong to the company so I created the next endpoint /company/1?include=employees, but I don’t know if I have to do a join in my model in order to get employees related with the company or every time I add an new employ modify the read model to get directly without not join. Right now I’m using the same tables for write model and read model.

Agustin Castro
  • 439
  • 1
  • 6
  • 20

1 Answers1

0

I don’t know if I have to do a join in my model in order to get employees related with the company or every time I add an new employ modify the read model to get directly without not join.

There are actually three choices.

1) When you add the new employee, also run the join query and use the results to update the read model; when you query the read model, just return the most recent copy.

2) When you add the new employee, stop. When you query the read model, run the join to update your data and then return this copy

3) When you add the new employee, stop. When you query the read model, just return the most recent copy. In the background, run a task that watches to see if a new employee was added - if so, run the join and update the read model.

Run in the background can mean a lot of different things - you can schedule a job each time an employee is added, you can run the job using a scheduler, you can give the admin controls to run the job on demand.

You'll probably end up making choices based on what kinds of SLA you need to meet (how "old" is the data in the read model allowed to be before people start complaining), dealing with how you are going to allow user to read their own writes, what other sorts of caching are in use in the system.

The important thing to understand is that "transform the write model to the read model" is an operation that you can run outside of the writes and reads.

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • 1) Should I do sync or async? I mean, once the event (EmployeAdded) happened and the new employ was added in the write model the event bus should execute the listener (UpdateCompanyQuery) sync or sent to rabbitmq executing async. 2) I will do in CompanyRepository select * from company c inner join company_employ ce... and return this result, is this right? 3) I think is the same that the option 1 but asyn, right? – Agustin Castro Jun 15 '18 at 07:49