public void Run(string[] args)
        {
            var ba         = new BankAccount();
            var cmdDeposit = new BankAccountCommand(ba,
                                                    BankAccountCommand.Action.Deposit, 100);
            var cmdWithdraw = new BankAccountCommand(ba,
                                                     BankAccountCommand.Action.Withdraw, 1000);

            cmdDeposit.Call();
            cmdWithdraw.Call();
            Console.WriteLine(ba);
            cmdWithdraw.Undo();
            cmdDeposit.Undo();
            Console.WriteLine(ba);


            var from = new BankAccount();

            from.Deposit(100);
            var to = new BankAccount();

            var mtc = new MoneyTransferCommand(from, to, 1000);

            mtc.Call();


            // Deposited $100, balance is now 100
            // balance: 100
            // balance: 0

            Console.WriteLine(from);
            Console.WriteLine(to);
        }
Example #2
0
        public void Init()
        {
            var assembly = typeof(ICoreAssemblyMarker).Assembly;
            var builder  = new ContainerBuilder();

            //Initialize all autofac modules in assembly
            builder.RegisterAssemblyModules(assembly);
            _container = builder.Build();

            _moneyTransfer = new MoneyTransferCommand
            {
                Amount            = 100,
                ReceiverAccountId = 1,
                SenderAccountId   = 2
            };

            _firstBankAccount = new GetAccountQueryById
            {
                AccountId = 1
            };

            _secondBankAccount = new GetAccountQueryById
            {
                AccountId = 2
            };
        }
        public void Init()
        {
            _bankAccountFrom = new TblBankAccounts
            {
                Balance = 100
            };

            _moneyTransferCommand = new MoneyTransferCommand
            {
                Amount = 120
            };
        }
        public void Call_TransferSmallerAmountThanBalance_ValidateToBalance()
        {
            var from = new BankAccount();

            from.Deposit(100);
            var to = new BankAccount();

            var mtc = new MoneyTransferCommand(from, to, 70);

            mtc.Call();

            Assert.That(to.Balance, Is.EqualTo(70));
        }
Example #5
0
        public void Try()
        {
            var from = new BankAccount(); from.Deposit(100);
            var to   = new BankAccount();

            Console.WriteLine(from);
            Console.WriteLine(to);

            var mtc = new MoneyTransferCommand(from, to, 1000);

            mtc.Apply();

            Console.WriteLine(from);
            Console.WriteLine(to);
        }
Example #6
0
        public void TransferMoney(string fromId, string toId, double amount)
        {
            if (IsItYourAccount(fromId))
            {
                throw new YourBankAccountException();
            }

            BankAccount fromAccount = FindAccount(fromId), toAccount = FindAccount(toId);

            var moneyTransferCommand = new MoneyTransferCommand(fromAccount, toAccount, amount);

            fromAccount.UpdateAccountHistory(moneyTransferCommand);
            toAccount.UpdateAccountHistory(moneyTransferCommand);

            moneyTransferCommand.Call();
        }
        public static void Test()
        {
            // var ba = new BankAccount();
            // var deposit = new BankAccountCommand(ba, BankAccountCommand.Action.Deposit, 100);
            // var withdraw = new BankAccountCommand(ba, BankAccountCommand.Action.Withdraw, 50);
            // var composite = new CompositeBankAccountCommand(
            //     new []{deposit, withdraw});
            //
            // composite.Call();
            // Console.WriteLine(ba);
            //
            // composite.Undo();
            // Console.WriteLine(ba);

            var from = new BankAccount();

            from.Deposit(100); // we have deposited 100 dollars to this account
            var to = new BankAccount();

            Console.WriteLine($"making transaction of 100 dollars");
            var mtc = new MoneyTransferCommand(from, to, 100); // making a transaction of 100 dollars to another account

            mtc.Call();

            Console.WriteLine(from); // the from account is left with 0 dollars
            Console.WriteLine(to);   // the to account has received the 100 dollars

            // undoes all successful transactions
            Console.WriteLine("Undoing previous transaction");
            mtc.Undo();
            Console.WriteLine(from);
            Console.WriteLine(to);

            // Suppose we make a transaction of 1000 dollars while the user from only has 100 dollars in his account
            // In this case, the Call() of the transaction will catch the error and will fail, undoing the whole
            // transaction in the process
            Console.WriteLine($"making transaction of 1000 dollars, which will fail, due to insufficient resources");
            var mtc2 = new MoneyTransferCommand(from, to, 1000);

            mtc2.Call();

            Console.WriteLine(from);
            Console.WriteLine(to);
        }
Example #8
0
        public void TestCompositeTransferMoney()
        {
            var from = new BankAccount();

            from.Deposit(1000);
            var to = new BankAccount();

            var mtc = new MoneyTransferCommand(from, to, 250);

            mtc.Call();

            Console.WriteLine(from);
            Console.WriteLine(to);


            mtc.Undo();

            Console.WriteLine(from);
            Console.WriteLine(to);
        }
Example #9
0
        static void Main(string[] args)
        {
            var from = new BankAccount();

            from.Deposit(100);

            var to = new BankAccount();


            var mtc = new MoneyTransferCommand(from, to, 100);

            mtc.Call();

            WriteLine(from);
            WriteLine(to);

            mtc.Undo();
            WriteLine(from);
            WriteLine(to);
        }
        public void Init()
        {
            var assembly = typeof(ICoreAssemblyMarker).Assembly;
            var builder  = new ContainerBuilder();

            //Initialize all autofac modules in assembly
            builder.RegisterAssemblyModules(assembly);
            _container = builder.Build();

            _moneyTransfer = new MoneyTransferCommand
            {
                Amount            = 100,
                ReceiverAccountId = 1,
                SenderAccountId   = 2
            };

            _notificationQuery = new GetNotificationForUserIdQuery
            {
                UserId = 1
            };
        }
            public void Test_money_transfer()
            {
                var from = new BankAccount();

                from.Deposit(100);
                var to = new BankAccount();

                Debug.WriteLine($"From account: {from}");
                Debug.WriteLine($"To account: {to}");

                var transfer = new MoneyTransferCommand(from, to, 100);

                transfer.Call();
                Debug.WriteLine($"From account: {from}");
                Debug.WriteLine($"To account: {to}");

                transfer.Undo();
                Debug.WriteLine($"From account: {from}");
                Debug.WriteLine($"To account: {to}");

                Assert.True(transfer.Succeeded);
            }
Example #12
0
 public IActionResult TransferMoney(MoneyTransferCommand moneyTransfer)
 {
     _commandBus.Send(moneyTransfer);
     return(Ok());
 }
 public IsTransferedAmountOfMoneyAvailableBusinessRule(TblBankAccounts bankAccountFrom, MoneyTransferCommand command)
 {
     this._bankAccountFrom      = bankAccountFrom;
     this._moneyTransferCommand = command;
 }