Guest User

Untitled

a guest
May 20th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. using System;
  2.  
  3. namespace EntityServiceDispatchThing
  4. {
  5. public interface ISubmitOrder
  6. {
  7. void Submit(IOrderSubmissionProcessor processor);
  8. }
  9.  
  10. public class Order : ISubmitOrder
  11. {
  12. private bool _submitted;
  13.  
  14. void ISubmitOrder.Submit(IOrderSubmissionProcessor processor)
  15. {
  16. if (_submitted) return; // or throw
  17. _submitted = true;
  18. Console.WriteLine("Order state has changed to submitted!");
  19.  
  20. processor.IssueFulfillmentRequestFor(this);
  21. // or we could go with PostProcessOrder in the case of multiple dependencies
  22. // or anti-corruption layers. point is this solution is for COMPLEX behavior
  23. }
  24. }
  25.  
  26. public interface IOrderSubmissionProcessor
  27. {
  28. void Submit(ISubmitOrder order);
  29. void IssueFulfillmentRequestFor(Order order);
  30. }
  31.  
  32. public class OrderSubmissionProcessor : IOrderSubmissionProcessor
  33. {
  34. private readonly IFulfillmentAntiCorruptionLayer _fulfillment;
  35.  
  36. public OrderSubmissionProcessor() : this(new FulfillmentAntiCorruptionLayer()) { }
  37.  
  38. public OrderSubmissionProcessor(IFulfillmentAntiCorruptionLayer fulfillment)
  39. {
  40. _fulfillment = fulfillment;
  41. }
  42.  
  43. public void Submit(ISubmitOrder order)
  44. {
  45. order.Submit(this);
  46. }
  47.  
  48. void IOrderSubmissionProcessor.IssueFulfillmentRequestFor(Order order)
  49. {
  50. var request = ConvertOrderToFulfillmentRequest(order);
  51. _fulfillment.Send(request);
  52. }
  53.  
  54. private FulfillmentRequest ConvertOrderToFulfillmentRequest(Order order)
  55. {
  56. // map the order to a fulfillment request
  57. // if the order changes or the fulfillment request changes
  58. // it'd be desirable for code to break here
  59. // we might even forward the mapping to our ACL eventually
  60. Console.WriteLine("Converting the order to a fulfillment request");
  61.  
  62. return new FulfillmentRequest();
  63. }
  64. }
  65.  
  66. public class FulfillmentRequest {}
  67.  
  68. public interface IFulfillmentAntiCorruptionLayer
  69. {
  70. void Send(FulfillmentRequest request);
  71. }
  72.  
  73. public class FulfillmentAntiCorruptionLayer : IFulfillmentAntiCorruptionLayer
  74. {
  75. public void Send(FulfillmentRequest request)
  76. {
  77. Console.WriteLine("fulfillment request sent to the legacy fulfillment system");
  78. }
  79. }
  80.  
  81. public class OrdersRepository
  82. {
  83. public Order FindBy(int id)
  84. {
  85. return new Order();
  86. }
  87. }
  88.  
  89. internal class Program
  90. {
  91. private static readonly OrdersRepository _orders = new OrdersRepository();
  92. private static readonly OrderSubmissionProcessor submissionProcessor = new OrderSubmissionProcessor();
  93.  
  94. private static void Main(string[] args)
  95. {
  96. var order = _orders.FindBy(42);
  97. submissionProcessor.Submit(order);
  98. Console.ReadLine();
  99. }
  100. }
  101. }
Add Comment
Please, Sign In to add comment