Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace EntityServiceDispatchThing
- {
- public interface ISubmitOrder
- {
- void Submit(IOrderSubmissionProcessor processor);
- }
- public class Order : ISubmitOrder
- {
- private bool _submitted;
- void ISubmitOrder.Submit(IOrderSubmissionProcessor processor)
- {
- if (_submitted) return; // or throw
- _submitted = true;
- Console.WriteLine("Order state has changed to submitted!");
- processor.IssueFulfillmentRequestFor(this);
- // or we could go with PostProcessOrder in the case of multiple dependencies
- // or anti-corruption layers. point is this solution is for COMPLEX behavior
- }
- }
- public interface IOrderSubmissionProcessor
- {
- void Submit(ISubmitOrder order);
- void IssueFulfillmentRequestFor(Order order);
- }
- public class OrderSubmissionProcessor : IOrderSubmissionProcessor
- {
- private readonly IFulfillmentAntiCorruptionLayer _fulfillment;
- public OrderSubmissionProcessor() : this(new FulfillmentAntiCorruptionLayer()) { }
- public OrderSubmissionProcessor(IFulfillmentAntiCorruptionLayer fulfillment)
- {
- _fulfillment = fulfillment;
- }
- public void Submit(ISubmitOrder order)
- {
- order.Submit(this);
- }
- void IOrderSubmissionProcessor.IssueFulfillmentRequestFor(Order order)
- {
- var request = ConvertOrderToFulfillmentRequest(order);
- _fulfillment.Send(request);
- }
- private FulfillmentRequest ConvertOrderToFulfillmentRequest(Order order)
- {
- // map the order to a fulfillment request
- // if the order changes or the fulfillment request changes
- // it'd be desirable for code to break here
- // we might even forward the mapping to our ACL eventually
- Console.WriteLine("Converting the order to a fulfillment request");
- return new FulfillmentRequest();
- }
- }
- public class FulfillmentRequest {}
- public interface IFulfillmentAntiCorruptionLayer
- {
- void Send(FulfillmentRequest request);
- }
- public class FulfillmentAntiCorruptionLayer : IFulfillmentAntiCorruptionLayer
- {
- public void Send(FulfillmentRequest request)
- {
- Console.WriteLine("fulfillment request sent to the legacy fulfillment system");
- }
- }
- public class OrdersRepository
- {
- public Order FindBy(int id)
- {
- return new Order();
- }
- }
- internal class Program
- {
- private static readonly OrdersRepository _orders = new OrdersRepository();
- private static readonly OrderSubmissionProcessor submissionProcessor = new OrderSubmissionProcessor();
- private static void Main(string[] args)
- {
- var order = _orders.FindBy(42);
- submissionProcessor.Submit(order);
- Console.ReadLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment