Search code examples
c#domain-driven-designpersistencecqrs

When do I persist my data in a non event-sourcing CQRS code base


I'm developing an architecture in C# based on the CQRS model. What I'm struggling to understand through the examples that I've come across is when to persist my data.

In some examples, including Microsoft's own items/cart example, they add their items to persist in a bus or whatever mechanism they use to transport the data outside the command.

And in other examples, the command contains the persistence, often stating that if the attempt to save fails, it's each to 're-trigger' the command and attempt the save again. While that argument has merit, doesn't that limit the amount of 'transactional' actions that can occur?


Solution

  • In a project that I've worked on, we persist the commands and events as soon as they were handled in CommandHandler or EventHandler. Also, the Commands and Events contained the read model in a de-normalized way (a JSON/AVRO format) for example, that was updated with new informations as soon as a new command/event was handled. At the end of the process, the end event contains the full read model. Another important aspect of doing in this way is that you are able to retry the handling of a specific command/event based on the previous commands/events that were handled successfully.