Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Place To Save Domain To Write Database In CQRS (Command Handler or Domain Event Handler)

I'm studying CQRS right now, and i see some source codes (Greg Young's SimpleCQRS and Mark Nihjof's). I still confuse with command and domain event. Do we always need to persist domain to "write database" in domain event handler? Is it common if I call the code to save the domain to database in command handler (usually through domain repository), and then let the domain event handler to handle other stuff (like: updating read model and do other services like email notification). Thanks.

like image 439
user522037 Avatar asked Jan 12 '11 05:01

user522037


1 Answers

Storing events: I wouldn't persist events using an event handler. Delegating it from a command handler to a repository or unit of work is probably the most common approach when using eventsourcing. So, yes it's common do the persistence in the commandhandler (well, delegate it) and have the event handler do other things.

Storing state: When not using eventsourcing, I presume people store events next to state or worse, not at all (using a queue as persistance mechanism). Still, persistence logic resides in the space of the commandhandler.

Commands capture intent and tell the system what to do. Always use the imperative. Events capture intent and tell what has happened in the system. Always in the past tense.

You strike me as someone new to this topic. Best thing you can do to grasp the concepts of CQRS is to watch material on http://cqrsinfo.com and http://skillsmatter.com (architecture/ddd). Other people that blog on this subject (off the top of my head): Udi Dahan, Gregory Young, Jonathan Oliver, Rinat Abdullin, Jérémie Chassaing, ...

like image 156
Yves Reynhout Avatar answered Sep 28 '22 06:09

Yves Reynhout