Guest User

Untitled

a guest
Feb 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace CQRS_CQS_Learning
  7. {
  8. class Program
  9. {
  10. public class Person
  11. {
  12. EventBroker broker;
  13. private int age;
  14.  
  15. public Person(EventBroker broker)
  16. {
  17. this.broker = broker;
  18. broker.Commands += BrokerOnCommands;
  19. broker.Queries += Broker_Queries;
  20. }
  21.  
  22. private void Broker_Queries(object sender, Query e)
  23. {
  24. var q = e as AgeQuery;
  25. if(q!=null && q.Target == this)
  26. {
  27. q.Result = age;
  28. }
  29. }
  30.  
  31. private void BrokerOnCommands(object sender, Command e)
  32. {
  33. var cac = e as ChangeAgeCommand;
  34. if (cac != null && cac.Target == this)
  35. {
  36. if (cac.Registred)
  37. {
  38. broker.AllEvents.Add(new AgeChangedEvent(this, age, cac.Age));
  39. }
  40. age = cac.Age;
  41. }
  42. }
  43. }
  44.  
  45. public class EventBroker
  46. {
  47. //1. All events 2. Commands 3. Queries
  48. public IList<Event> AllEvents = new List<Event>();
  49. public event EventHandler<Command> Commands;
  50. public event EventHandler<Query> Queries;
  51.  
  52. public void Command(Command c)
  53. {
  54. Commands?.Invoke(this, c);
  55. }
  56.  
  57. public T Query<T>(Query q)
  58. {
  59. Queries?.Invoke(this, q);
  60. return (T)q.Result;
  61. }
  62.  
  63. public void UndoLast()
  64. {
  65. var e = AllEvents.LastOrDefault();
  66. var ac = e as AgeChangedEvent;
  67. if (ac != null)
  68. {
  69. Command(new ChangeAgeCommand(ac.Target, ac.OldAge) { Registred = false });
  70. AllEvents.Remove(e);
  71. }
  72. }
  73. }
  74.  
  75.  
  76. public class ChangeAgeCommand :Command
  77. {
  78. public Person Target;
  79. public int Age;
  80.  
  81. public ChangeAgeCommand(Person target, int age) {
  82. Target = target;
  83. Age = age;
  84. }
  85. }
  86.  
  87. public class Query
  88. {
  89. public object Result;
  90. }
  91.  
  92. public class AgeQuery:Query
  93. {
  94. public Person Target;
  95. }
  96.  
  97. public class Command: EventArgs
  98. {
  99. public bool Registred = true;
  100. }
  101.  
  102. public class Event
  103. {
  104.  
  105. }
  106.  
  107. public class AgeChangedEvent:Event
  108. {
  109. public Person Target;
  110. public int OldAge, NewAge;
  111.  
  112. public AgeChangedEvent(Person target, int oldAge, int newAge)
  113. {
  114. Target = target;
  115. OldAge = oldAge;
  116. NewAge = newAge;
  117. }
  118.  
  119. public override string ToString()
  120. {
  121. return $"Age changed from {OldAge} to {NewAge}";
  122. }
  123. }
  124.  
  125. static void Main(string[] args)
  126. {
  127. var eb = new EventBroker();
  128. var p = new Person(eb);
  129.  
  130. eb.Command(new ChangeAgeCommand(p, 123));
  131. eb.Command(new ChangeAgeCommand(p, 55));
  132. eb.Command(new ChangeAgeCommand(p, 21));
  133. eb.Command(new ChangeAgeCommand(p, 3));
  134. eb.Command(new ChangeAgeCommand(p, 77));
  135.  
  136. foreach (var e in eb.AllEvents)
  137. {
  138. Console.WriteLine(e);
  139. }
  140.  
  141. int age = eb.Query<int>(new AgeQuery { Target = p });
  142.  
  143. Console.WriteLine(age);
  144.  
  145. eb.UndoLast();
  146.  
  147. age = eb.Query<int>(new AgeQuery { Target = p });
  148. Console.WriteLine(age);
  149.  
  150. Console.ReadKey();
  151. }
  152. }
  153. }
Add Comment
Please, Sign In to add comment