ลองมาพิจารณาข้อดีของการใช้ Commands (4, 14) และ Command Handlers เพื่อควบคุมการจัดการงานของแอปพลิเคชันของเรา เริ่มจากลองย้อนกลับไปดู Application Service ของเรากับ method LockCustomer() ของมันอีกครั้ง:

public class CustomerApplicationService
{
  ...
  public void LockCustomer(CustomerId id, string reason)
  {
    var eventStream = _eventStore.LoadEventStream(id);
    var customer = new Customer(stream.Events);
    customer.LockCustomer(reason);
    _store.AppendToStream(id, eventStream.Version, customer.Changes);
  }
  ...
}

ทีนี้ลองจินตนาการถึงการสร้าง representation แบบ serialized ของชื่อ method และ parameter ต่าง ๆ ของมัน มันจะมีหน้าตาเป็นแบบไหนกัน? เรา อาจสร้าง class ที่ตั้งชื่อตาม operation ของแอปพลิเคชัน และสร้าง instance properties ที่ตรงกับ parameter ต่าง ๆ ของ method ใน service ได้ class แบบนี้เองที่กลายมาเป็น Command หนึ่งตัว:


public sealed class LockCustomerCommand
{
  public CustomerId { get; set; }
  public string Reason { get; set; }
}


Command contracts มี semantics เดียวกับ Events และยังสามารถแชร์ใช้งานข้ามระบบได้ในลักษณะเดียวกัน จากนั้น Command ตัวนี้ก็จะถูกส่งต่อให้ method บน Application Service:

public class CustomerApplicationService
{
  ...
  public void When(LockCustomerCommand command)
  {
    var eventStream = _eventStore.LoadEventStream(command.CustomerId);
    var customer = new Customer(stream.Events);
    customer.LockCustomer(command.Reason);
    _eventStore.AppendToStream(
      command.CustomerId, eventStream.Version, customer.Changes);
  }
  ...
}

การ refactor ง่าย ๆ แบบนี้ให้ประโยชน์ระยะยาวกับระบบได้หลายอย่าง มาดูกันว่ามันเป็นอย่างไร

เนื่องจาก object ของ Command สามารถถูกทำให้เป็น serialized ได้ เราจึงสามารถส่ง representation แบบ text หรือ binary เป็น message ไปบน message queue ได้ โดย object ที่ได้รับ message นั้นคือ message handler ซึ่งสำหรับเรา ก็คือ Command Handler นั่นเอง Command Handler เข้ามาทำหน้าที่แทนที่ method ของ Application Service โดยแท้จริง แม้ว่ามันจะเทียบเท่ากันคร่าว ๆ และอาจยังถูกเรียกแบบนั้นอยู่ก็ตาม ยังไงก็ตาม การลดการผูกติด (decoupling) ของ client กับ Service ช่วยยกระดับ load balancing, เปิดทางให้เกิด competing consumers และรองรับการแบ่งส่วนระบบ (system partitioning) ได้ ยก load balancing ขึ้นมาเป็นตัวอย่าง เราสามารถกระจายโหลดได้โดยรัน Command Handler ตัวเดียวกัน (ซึ่งโดย semantics แล้วก็คือ Application Service) บนเซิร์ฟเวอร์กี่เครื่องก็ได้ เมื่อ Commands ถูกวางลงบน message queue แล้ว message ของ Command เหล่านั้นก็จะถูกส่งไปยัง Command Handler ตัวใดตัวหนึ่งในหลายตัวที่กำลังคอยฟัง (listen) อยู่ ดังที่ Figure A.8 แสดงไว้ (ใน appendix นี้ Commands จะแสดงเป็น object ทรงกลม) การกระจายจริง ๆ อาจทำได้โดยใช้วิธีง่าย ๆ อย่าง round-robin หรืออัลกอริทึมการส่งมอบที่ซับซ้อนกว่านั้น ซึ่งทั้งหมดนี้เป็นสิ่งที่ messaging infrastructure จัดหาไว้ให้

Image

Figure A.8. Application Commands ถูกกระจายไปยัง Command Handlers จำนวนเท่าใดก็ได้

แนวทางนี้สร้าง temporal decoupling (การลดการผูกติดทางเวลา) ระหว่าง client กับ Application Service ซึ่งนำไปสู่ระบบที่แข็งแกร่งขึ้น ตัวอย่างแรก client จะไม่ถูก block อีกต่อไปเมื่อ Application Service ไม่พร้อมใช้งานชั่วระยะเวลาสั้น ๆ (เช่น ตอนซ่อมบำรุง หรือ อัปเกรดระบบ) แต่ Commands จะถูกเก็บไว้ใน persistent queue ซึ่งจะถูกประมวลผลโดย Command Handlers (Application Service) เมื่อเซิร์ฟเวอร์ของมันกลับมาออนไลน์อีกครั้ง ตามที่ระบุใน Figure A.9

ข้อดีอีกข้อคือความสามารถในการ chain aspect เพิ่มเติมก่อนการ dispatch Command ได้ตามต้องการ ตัวอย่างเช่น เรา สามารถแทรก (patch) auditing, logging, authorization และ validation เข้าไปได้อย่างง่ายดาย

Image

Figure A.9. ลักษณะ temporal decoupling ของ Commands แบบ message-based และ Command Handlers ของมัน เปิดทางให้มีตัวเลือกด้าน availability ของระบบที่ยืดหยุ่น

มาดูกันว่าเราจะแทรก logging เข้าไปได้อย่างไรบ้าง ก่อนอื่นเราจะนิยาม interface มาตรฐานขึ้นมา แล้ว implement interface นั้นใน class ของ Application Service:

public interface IApplicationService
{
    void Execute(ICommand cmd);
}

public partial class CustomerApplicationService : IApplicationService
{
  public void Execute(ICommand command)
  {
    // pass command to a specific method When()
    // that can handle the command
    ((dynamic)this).When((dynamic)command);
  }
}

Execute และ Mutate ต่างก็มี Implementation ที่คล้ายกัน

สังเกตว่าการ implement method Execute() นี้มีลักษณะบางอย่างคล้ายกับ method Mutate() ที่อธิบายไว้ก่อนหน้านี้ในส่วนของการออกแบบ Aggregate แบบ A+ES


เมื่อเรามี interface มาตรฐานสำหรับ Command Handlers (Application Services) ทั้งหมดแล้ว เราก็สามารถ แทรก feature มาตรฐานแบบ pre- และ post-execution ชนิดใดก็ได้ เช่น generic logging:

public class LoggingWrapper : IApplicationService
{
  readonly IApplicationService _service;

  public LoggingWrapper(IApplicationService service)
  {
    _service = service;
  }

  public void Execute(ICommand cmd)
  {
    Console.WriteLine("Command: " + cmd);
    try
    {
      var watch = Stopwatch.StartNew();
      _service.Execute(cmd);
      var ms = watch.ElapsedMilliseconds;
      Console.WriteLine("  Completed in {0} ms", ms);
    }
    catch( Exception ex)
    {
      Console.WriteLine("Error: {0}", ex);
    }
  }
}

เนื่องจาก Application Services ทั้งหมดมี interface มาตรฐานเดียวกัน เราจึงสามารถแทรก utility ทั่วไปได้ ไม่จำกัดจำนวน ไม่ว่าจะเป็นตัวที่ทำงานก่อนและ/หรือหลังฟังก์ชันของ Command Handler จริง ๆ นี่คือวิธีที่ CustomerApplicationService ถูก initialize พร้อมกับ logging แบบ pre- และ post-execution:

var customerService =
  new CustomerApplicationService(eventStore, pricingService);
var customerServiceWithLogging = new LoggingWrapper(customerService);

แน่นอนว่า การที่ Commands เป็น object แบบ serialized ที่ถูกส่งไปยัง Command Handlers ทำให้เราสามารถจัดการ กับความล้มเหลว (failure) และเงื่อนไข error ต่าง ๆ ได้ในที่เดียว เมื่อ error อยู่ในกลุ่มใดกลุ่มหนึ่ง เช่น ปัญหา resource contention ที่เกิดจากเรื่อง concurrency เราก็สามารถเลือกการกู้คืน (recovery) แบบมาตรฐานได้ เช่น การลอง execute operation ซ้ำ X ครั้ง โดยการ retry อาจอิงกับกลยุทธ์ Capped Exponential Back-off ซึ่งทำให้การ retry ทั้งหมดสม่ำเสมอ น่าเชื่อถือ และถูกดูแลอยู่ใน class เดียว