Exemplo n.º 1
0
        public IEnumerable <AggregateEvent> TransferMoney(TransferMoneyCommand command)
        {
            if (this.CurrentBalance < command.Amount)
            {
                throw new InvalidOperationException($"Account {AccountNumber} does not have enough balance to execute the transaction.");
            }
            if (this.AccountState != AccountState.Created)
            {
                throw new InvalidOperationException($"Account {AccountNumber} is not available.");
            }

            var destinationAccountEvents = this.EventStore.GetAggregateEvents(CreateAggregateId(command.DestinationAccountNumber));
            var destinationAccount       = new Account(this.EventStore, destinationAccountEvents);

            if (destinationAccount.AccountState != AccountState.Created)
            {
                throw new InvalidOperationException($"Account {destinationAccount.AccountNumber} is not available.");
            }

            var aggregateEvent = new BalanceIncreased(command.DestinationAccountNumber, ++destinationAccount.SequenceNumber, command.Amount);

            Handle(aggregateEvent);

            return(new AggregateEvent[]
            {
                aggregateEvent,
                new BalanceDecreased(command.SourceAccountNumber, ++this.SequenceNumber, command.Amount)
            });
        }
Exemplo n.º 2
0
        public IEnumerable <AggregateEvent> MakeDeposit(MakeDepositCommand command)
        {
            if (this.AccountState != AccountState.Created)
            {
                throw new InvalidOperationException($"Account {AccountNumber} is not available.");
            }

            var aggregateEvent = new BalanceIncreased(command.AccountNumber, ++SequenceNumber, command.Amount);

            Handle(aggregateEvent);

            return(new AggregateEvent[]
            {
                aggregateEvent
            });
        }
Exemplo n.º 3
0
 public void Handle(BalanceIncreased request)
 {
     this.CurrentBalance += request.Amount;
     this.SequenceNumber  = request.SequenceNumber;
 }