Esempio n. 1
0
        private bool Active(object message)
        {
            return(base.ReceiveCommand(message) || message.Match()
                   .With <AccountCommands.DeactivateAccount>(deactivate =>
            {
                Persist(new AccountEvents.AccountDeactivated(deactivate.AccountId, Clock()));
            })
                   .With <AccountCommands.Deposit>(deposit =>
            {
                if (deposit.Amount > 0)
                {
                    Persist(new AccountEvents.Deposited(_id, deposit.Amount, Clock()));
                }
                else
                {
                    Log.Error("Cannot perform deposit on account {0}: money amount is not positive value", _id);
                }
            })
                   .With <AccountCommands.Withdraw>(withdraw =>
            {
                var sender = Sender;
                var withdrawal = new AccountEvents.Withdrawal(_id, withdraw.Amount, Clock());

                // Use defer to await to proceed command until all account events have been
                // persisted and handled. This is done mostly, because we don't want to perform
                // negative account check while there may be still account balance modifying events
                // waiting in mailbox.
                Defer(withdrawal, e =>
                {
                    if (withdraw.Amount > 0 && withdraw.Amount <= State.Balance)
                    {
                        Persist(e, sender);
                    }
                    else
                    {
                        Log.Error("Cannot perform withdrawal from account {0}, because it has a negative balance", _id);
                        sender.Tell(new NotEnoughtFunds(_id));
                    }
                });
            })
                   .With <AccountCommands.Transfer>(transfer =>
            {
                //TODO: transfer
            })
                   .WasHandled);
        }
Esempio n. 2
0
        private void Withdraw(AccountCommands.Withdraw withdraw)
        {
            var sender     = Sender;
            var withdrawal = new AccountEvents.Withdrawal(_id, withdraw.Amount, Clock());

            // Use defer to await to proceed command until all account events have been
            // persisted and handled. This is done mostly, because we don't want to perform
            // negative account check while there may be still account balance modifying events
            // waiting in mailbox.
            Defer(withdrawal, e =>
            {
                if (withdraw.Amount > 0 && withdraw.Amount <= (State.Balance - ReservedFunds))
                {
                    Persist(e, sender);
                }
                else
                {
                    Log.Error("Cannot perform withdrawal from account {0}, because it has a negative balance", _id);
                    sender.Tell(new NotEnoughtFunds(_id));
                }
            });
        }