I’m currently teaching myself event sourcing and have got the concept enough to start developing a dummy app in C# and EventStore. My app is an easy-to-understand bank account system.
If we model a bank account as a series of withdrawal and deposit events then we can easily calculate the account’s current balance by adding up all the deposits and subtracting all the withdrawals, and EventStore gives us a way to implement this in the data store using projections.
However, if we have a business rule that dictates that accounts can never go overdrawn (i.e. we must reject requests to withdraw from an account if the current balance is insufficient to cover the requested funds), then our BankAccount entity also needs to know its current balance.
Again, this is easy to calculate by running over the series of bank account events but I have a deep unease with the fact that I have had to implement my projection of “balance” twice: once in the data store and once in the domain.
Is this expected from Event Sourcing, and I shouldn’t worry about it? Or am I going down the wrong path, and in fact the balance should not be calculated in the domain code and should instead be retrieved from the data store every time it is required? This means that a round trip to the data store would be required every time an entity is modified.
I’ve seen in Greg Young’s talks that it’s supposed to be a natural fit for DDD so there shouldn’t be an incompatibility there.
If I’ve misunderstood any part of this set up then any help would be greatly appreciated.