Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //RoleContract
- public interface ISourceAccount : IRolePlayer
- {
- void DrawAmount(decimal amount);
- bool IsEnoughAmountToTransfer(decimal amount);
- }
- //RoleContract
- public interface IDestinationAccount : IRolePlayer
- {
- void ReceiveAmount(decimal amount);
- }
- //Data - ValueObject
- public class Account
- {
- public decimal AvailableBalance { get; set; }
- public decimal LedgerBalance { get; set; }
- }
- //Data & Player
- public class InvestmentAccount : IRolePlayer
- {
- public Account Account {get;set;}
- public void DrawAmount(decimal amount)
- {
- this.Account.AvailableBalance -= amount;
- }
- public void ReceiveAmount(decimal amount)
- {
- this.Account.AvailableBalance += amount;
- }
- public bool IsEnoughAmountToTransfer(decimal amount)
- {
- if (Account.AvailableBalance >= amount)
- return true;
- return false;
- }
- }
- //Data & Player
- public class SavingAccount : IRolePlayer
- {
- public Account Account { get; set; }
- public void DrawAmount(decimal amount)
- {
- this.Account.AvailableBalance -= amount;
- }
- public void ReceiveAmount(decimal amount)
- {
- this.Account.AvailableBalance += amount;
- }
- public bool IsEnoughAmountToTransfer(decimal amount)
- {
- if (Account.AvailableBalance >= amount + 10m)
- return true;
- return false;
- }
- }
- //Context
- public class TransferMoneyContext : Context
- {
- public ISourceAccount SourceAccount { get; set; }
- public IDestinationAccount SinkAccount { get; set; }
- public TransferMoneyContext Bind(IRolePlayer sourceAccount, IRolePlayer sinkAccount)
- {
- SourceAccount = sourceAccount.As<ISourceAccount>();
- SinkAccount = sinkAccount.As<IDestinationAccount>();
- return this;
- }
- public void Transfer(decimal amount)
- {
- var sourceAccount = SourceAccount;
- var destAccount = SinkAccount;
- sourceAccount
- .RoleAsTransferMoney(c => c.DoIt(destAccount, amount));
- }
- }
- //RoleMethod
- public static class RegisterRoleAs
- {
- public static void RoleAsTransferMoney(this ISourceAccount self, Action<TransferMoney> action)
- {
- action(self.MapTo<TransferMoney>());
- }
- //Role
- public class TransferMoney : PlayedBy<ISourceAccount>
- {
- //RoleMethod
- public void DoIt(IDestinationAccount destAccount, decimal amount)
- {
- if (self.IsEnoughAmountToTransfer(amount))
- {
- self.DrawAmount(amount);
- destAccount.ReceiveAmount(amount);
- }
- else
- {
- Console.WriteLine("Insufficient amount!");
- }
- }
- }
- }
- //Run
- public class MoneyTransferApp
- {
- public static void Main(string[] args)
- {
- var sourceAccount = new SavingAccount { Account = new Account { AvailableBalance = 100m } };
- var destAccount = new InvestmentAccount { Account = new Account { AvailableBalance = 30m } };
- var ctx = new ContextFactory();
- ctx.RunAs<TransferMoneyContext>()
- .Bind(sourceAccount,destAccount)
- .Transfer(10m);
- }
- }
Add Comment
Please, Sign In to add comment