Guest User

Untitled

a guest
Oct 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. //RoleContract
  2. public interface ISourceAccount : IRolePlayer
  3. {
  4. void DrawAmount(decimal amount);
  5.  
  6. bool IsEnoughAmountToTransfer(decimal amount);
  7. }
  8.  
  9. //RoleContract
  10. public interface IDestinationAccount : IRolePlayer
  11. {
  12. void ReceiveAmount(decimal amount);
  13. }
  14.  
  15. //Data - ValueObject
  16. public class Account
  17. {
  18. public decimal AvailableBalance { get; set; }
  19.  
  20. public decimal LedgerBalance { get; set; }
  21. }
  22.  
  23. //Data & Player
  24. public class InvestmentAccount : IRolePlayer
  25. {
  26. public Account Account {get;set;}
  27.  
  28. public void DrawAmount(decimal amount)
  29. {
  30. this.Account.AvailableBalance -= amount;
  31. }
  32.  
  33. public void ReceiveAmount(decimal amount)
  34. {
  35. this.Account.AvailableBalance += amount;
  36. }
  37.  
  38.  
  39. public bool IsEnoughAmountToTransfer(decimal amount)
  40. {
  41. if (Account.AvailableBalance >= amount)
  42. return true;
  43. return false;
  44. }
  45. }
  46.  
  47. //Data & Player
  48. public class SavingAccount : IRolePlayer
  49. {
  50. public Account Account { get; set; }
  51.  
  52. public void DrawAmount(decimal amount)
  53. {
  54. this.Account.AvailableBalance -= amount;
  55. }
  56.  
  57. public void ReceiveAmount(decimal amount)
  58. {
  59. this.Account.AvailableBalance += amount;
  60. }
  61.  
  62.  
  63. public bool IsEnoughAmountToTransfer(decimal amount)
  64. {
  65. if (Account.AvailableBalance >= amount + 10m)
  66. return true;
  67. return false;
  68. }
  69. }
  70.  
  71. //Context
  72. public class TransferMoneyContext : Context
  73. {
  74. public ISourceAccount SourceAccount { get; set; }
  75.  
  76. public IDestinationAccount SinkAccount { get; set; }
  77.  
  78. public TransferMoneyContext Bind(IRolePlayer sourceAccount, IRolePlayer sinkAccount)
  79. {
  80. SourceAccount = sourceAccount.As<ISourceAccount>();
  81. SinkAccount = sinkAccount.As<IDestinationAccount>();
  82. return this;
  83. }
  84.  
  85. public void Transfer(decimal amount)
  86. {
  87. var sourceAccount = SourceAccount;
  88.  
  89. var destAccount = SinkAccount;
  90.  
  91. sourceAccount
  92. .RoleAsTransferMoney(c => c.DoIt(destAccount, amount));
  93.  
  94. }
  95. }
  96.  
  97. //RoleMethod
  98. public static class RegisterRoleAs
  99. {
  100. public static void RoleAsTransferMoney(this ISourceAccount self, Action<TransferMoney> action)
  101. {
  102. action(self.MapTo<TransferMoney>());
  103. }
  104.  
  105. //Role
  106. public class TransferMoney : PlayedBy<ISourceAccount>
  107. {
  108. //RoleMethod
  109. public void DoIt(IDestinationAccount destAccount, decimal amount)
  110. {
  111. if (self.IsEnoughAmountToTransfer(amount))
  112. {
  113. self.DrawAmount(amount);
  114. destAccount.ReceiveAmount(amount);
  115. }
  116. else
  117. {
  118. Console.WriteLine("Insufficient amount!");
  119. }
  120. }
  121. }
  122.  
  123. }
  124.  
  125. //Run
  126. public class MoneyTransferApp
  127. {
  128. public static void Main(string[] args)
  129. {
  130. var sourceAccount = new SavingAccount { Account = new Account { AvailableBalance = 100m } };
  131.  
  132. var destAccount = new InvestmentAccount { Account = new Account { AvailableBalance = 30m } };
  133.  
  134. var ctx = new ContextFactory();
  135. ctx.RunAs<TransferMoneyContext>()
  136. .Bind(sourceAccount,destAccount)
  137. .Transfer(10m);
  138. }
  139. }
Add Comment
Please, Sign In to add comment