private void Handle(CommitTransaction cmd) { var sender = Sender; if (cmd.AccountNumber != _state.AccountNumber) { throw new NotFoundException("Invalid Account Number"); } if (!_state.OutstandingTransaction.ContainsKey(cmd.TransactionId)) { // command idempotent, just confirm that it is done Respond(sender, "Commited"); return; } var amount = _state.OutstandingTransaction[cmd.TransactionId]; var commit = new AccountDebitCommited { TransactionId = cmd.TransactionId, Amount = amount, EndedDate = DateTime.UtcNow }; Persist(commit, e => { ApplyEvent(e); Respond(sender, "Commited", new[] { commit }); }); }
public void Apply(AccountDebitCommited @event) { Balance -= @event.Amount; OutstandingTransaction.Remove(@event.TransactionId); }