MoneyTransfer Context - Transferring money between two accounts.
Beispiel #1
0
        /// <summary>
        /// Use case implementation of PayBills.
        /// </summary>
        public static void PayBills(this PayBills.SourceAccountRole sourceAccount)
        {
            // Get the current Context first.
            var c = Context.Current <PayBills>(sourceAccount, ct => ct.SourceAccount);

            // Get the current Creditor list, enumerating it to a list so it's not modified during the operation.
            var creditors = c.Creditors.ToList();

            // If not enough money to pay all creditors, don't pay anything.
            var surplus = sourceAccount.Balance - creditors.Sum(cr => cr.AmountOwed);

            if (surplus < 0)
            {
                throw new AccountException(
                          string.Format("Not enough money on account to pay all bills.\r\n{0:c} more is needed.", Math.Abs(surplus)));
            }

            creditors.ForEach(creditor =>
            {
                // For each creditor, create a MoneyTransfer Context.
                var transferContext = new MoneyTransfer(sourceAccount, creditor.Account, creditor.AmountOwed);

                // When the whole context object can be supplied to Context.Execute, there
                // is no need to set the context explicitly. Context.Execute will look for
                // a method called Execute and invoke it.
                //
                // If another method than Execute must be invoked, use the Action overload.
                Context.Execute(transferContext);
            });
        }
Beispiel #2
0
        /// <summary>
        /// First example. Please open the MoneyTransfer.cs file for an in-depth
        /// explanation of a Context.
        /// </summary>
        private static void BasicMoneyTransfer(Account source, Account destination)
        {
            // Create the context using the supplied accounts. 245 will be the transfer amount.
            var context = new Contexts.MoneyTransfer(source, destination, 245m);

            Console.WriteLine("BEFORE");
            Console.WriteLine("Source account           " + source.Balance.ToString("c"));
            Console.WriteLine("Destination account      " + destination.Balance.ToString("c"));

            Console.WriteLine();
            Console.WriteLine("Transfering from Source to Destination: " + context.Amount.ToString("c"));

            // Execute the context.
            context.Execute();

            Console.WriteLine();
            Console.WriteLine("AFTER");
            Console.WriteLine("Source account           " + source.Balance.ToString("c"));
            Console.WriteLine("Destination account      " + destination.Balance.ToString("c"));
        }