I was looking for an easy implementation of the Event Sourcing and CQRS Pattern in ASP.NET Core using Azure Cosmos DB as Event Store.
As I couldn't find anything appropriate I decided to write a solution using the mentioned technology stack on my own.
The ASP.NET Core Backend receives commands, creates events and writes it in the event store. The Azure Function processes incoming events and calculates a materialized view containing an account with the current balance and status based on the events received.
I put the source code on GitHub: https://github.com/marxxxx/EventSourcingCqrsDemo
I would say, it's basically working but there is one thing I did not understand enough to get it right.
One of the domain rules is: You can only transfer as much money as it is available on your account. I put this check in the domain object (Account). However, as the Domain Object is just used in the Azure Function to calculate the latest version of the Materialized View, an error would just be raised when processing the event. The user of the Web Api, issuing the faulty transfer is not informed directly as part of the HTTP response.
So my questions would be: - Is it common when using this kind of patterns to duplicate the domain rule check if I need instant execution of domain rules as part of the request? - Is my understanding of commands vs events wrong? Did I use the domain object in the wrong place? Should I use it when handling the incoming request/command to generate the domain events?
Thank you very much for any feedback.