requesting data from the readmodel in command handler

348 views Asked by At

I have the need to send an email to a list of admins as a response to a command sent from the UI. The admins that need to be notified are retrieved from a query against the read model.

At the moment - from my understanding as it stands I only have one option: gather the required admins before sending the command and add them (name/email pair) to the command.

Or - is it acceptable to cross from the command handler to the read model? It doesn't feel right but I don't really see any other way of doing this?

3

There are 3 answers

0
Fossmo On

You don't tell anything about how you have implemented the command side. I usually have this information available in the command handler. I then query the datasource for e-mail and name based on the IDs added to the command. You don't need to go through the domain model to do this. If you don't have the data available on the command side, I don't see a big problem with collecting data from the read side through a service. I would not do this from the domain model.

1
Alexandre Roba On

You simply cannot access the read model from your domain as your domain is temporal, not your read model. You need to have the list of email's that were taken athe time of the command :)

You can, or include on the commands that trigger the mail the list of admin's email, It is then persisted as an event and temporal, Or you could have your domain entity instantiating other domain entity that will get the the list admin emails.

Hope this help.

0
Chris Moutray On

Assuming your commands are throwing events...

To me the right way to go about this would be to have a separate event handler to capture the command's event and trigger the sending of the email. Your event handler would query the existing read-model for a list of admins.

Keeps things simple as your command handler doesn't need to be aware of this need to email the admins.

You could go the extreme of conceptually having a separate system for this, which also handles your admin-created/deleted events, storing in a specific read-model purely to hold a list of admins to send the email to.