DDD: Why calculate balance in event, but not in state mutator?

Hello!
https://hexdocs.pm/commanded/aggregates.html

# state mutators

  def apply(%BankAccount{} = state, %MoneyWithdrawn{balance: balance}),
    do: %BankAccount{state | balance: balance}

  def apply(%BankAccount{} = state, %AccountOverdrawn{}),
    do: %BankAccount{state | state: :overdrawn}

  # private helpers

  defp withdraw_money(%BankAccount{account_number: account_number, balance: balance}, amount) do
    %MoneyWithdrawn{
      account_number: account_number,
      amount: amount,
      balance: balance - amount
    }
  end

I am going through commanded guides and this thing is sooooo curious!
What is the point to store new balance in event? Logically, state mutator should calculate new balance from amount in MoneyWithdrawn event?
Thanks

Hi! And welcome :smiley:

It is difficult to say a-priori with no knowledge of the domain you are working on.

Probably it is not needed.

Probably it is needed because at some point you want to publish this event and have another service that is only listening to event like MoneyWithdrawn and do some logic with its current balance. I mean, it could be there because of strategical reasons.

I would say that the things you decide to put in your events should be dictated by the domain you are working on. Usually, the information you put in the event are not meant to solve technical issues.

In your specific case, it’s just an example on how commanded can be used.

Thank you.
I was going further and have found kind of explanation.

Notice that the event doesn’t have the amount of the deposit or withdrawal, but just the new balance of the aggregate. This makes our lives easier when projecting these events onto the read-model.

Hmmm…