/// <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); }); }
// When the Context is created, the Roles will be bound using the supplied arguments. // There can be multiple constructors with different ways of retreiving the objects // needed for the binding (database lookup, web service, etc), but there can only be // one BindRoles method, which does the final Role binding. // The constructor(s) should be strongly typed to check for errors. public MoneyTransfer(PayBills.SourceAccountRole source, Account destination, decimal amount) { BindRoles(source, destination, amount); }