ดังที่เรา ได้พูดถึงก่อนหน้านี้ ตามอุดมคติแล้วเราอยากให้ function ทั้งหมดของเราเป็น “pure” เพราะจะทำให้เข้าใจและทดสอบได้ง่ายขึ้น function ที่อ่านจากหรือเขียนไปยังโลกภายนอกไม่สามารถเป็น pure ได้ ดังนั้นเมื่อออกแบบ workflow เราจึงต้องการหลีกเลี่ยง logic ที่เกี่ยวข้องกับ I/O หรือ persistence ใดๆ ภายใน workflow โดยทั่วไปแล้วนี่หมายถึงการแยก workflow ออกเป็นสองส่วน:

  • ส่วนที่เน้น domain เป็นศูนย์กลาง ซึ่งมี business logic อยู่
  • ส่วน “ขอบ” (edge) ซึ่งมี code ที่เกี่ยวข้องกับ I/O

ตัวอย่างเช่น สมมติว่าเรามี workflow ที่ implement logic สำหรับการจ่ายบิล (invoice) ในโมเดลที่ผสม domain logic กับ I/O เข้าด้วยกัน implementation อาจถูกออกแบบแบบนี้:

  • โหลด invoice จาก database

  • ใช้การชำระเงิน (payment)

  • ถ้า invoice ถูกจ่ายครบแล้ว ให้ทำเครื่องหมายว่าจ่ายครบใน database และโพสต์ event InvoicePaid

  • ถ้า invoice ยังจ่ายไม่ครบ ให้ทำเครื่องหมายว่าจ่ายบางส่วนใน database และไม่ต้องโพสต์ event ใดๆ

นี่คือหน้าตาของ code นั้นใน F#:

​  ​ // workflow mixes domain logic and I/O ​
​  ​ let ​ payInvoice invoiceId payment =
​  ​ // load from DB ​
​  ​ let ​ invoice = loadInvoiceFromDatabase(invoiceId)
​  
​  ​ // apply payment ​
​  invoice.ApplyPayment(payment)
​  
​  ​ // handle different outcomes ​
​  ​ if ​ invoice.IsFullyPaid ​ then ​
​  markAsFullyPaidInDb(invoiceId)
​  postInvoicePaidEvent(invoiceId)
​  ​ else ​
​  markAsPartiallyPaidInDb(invoiceId)

ปัญหาคือ function นี้ไม่ใช่ pure และจะทดสอบได้ยาก

ลองแยก business logic ที่เป็น pure ออกมาเป็น function applyPayment ที่ไม่แตะ database เลย แต่จะคืนค่าเป็นการตัดสินใจว่าจะทำอะไรต่อไปแทน ซึ่งเราจะเรียกมันว่า InvoicePaymentResult :

​  ​ type ​ InvoicePaymentResult =
​  | FullyPaid
​  | PartiallyPaid ​ of ​ ...
​  
​  ​ // domain workflow: pure function ​
​  ​ let ​ applyPayment unpaidInvoice payment :InvoicePaymentResult =
​  ​ // apply payment ​
​  ​ let ​ updatedInvoice = unpaidInvoice |> applyPayment payment
​  
​  ​ // handle different outcomes ​
​  ​ if ​ isFullyPaid updatedInvoice ​ then ​
​  FullyPaid
​  ​ else ​
​  PartiallyPaid updatedInvoice
​  ​ // return PartiallyPaid or FullyPaid ​

function นี้เป็น pure อย่างสมบูรณ์ มันไม่โหลดข้อมูลใดๆ เลย—ข้อมูลทั้งหมดที่มันต้องการถูกส่งเข้ามาเป็นพารามิเตอร์ และมันก็ไม่บันทึกข้อมูลใดๆ ด้วย มันตัดสินใจแล้วคืนค่าการตัดสินใจนั้นออกมาเป็น choice type อย่าง InvoicePaymentResult แทนที่จะลงมือทำทันที ผลก็คือ มันง่ายที่จะทดสอบว่า logic ใน function นี้ทำงานถูกต้อง

เมื่อเขียน function นี้เสร็จแล้ว เราจะใช้มันเป็นส่วนหนึ่งของ command handler ที่ขอบเขต (boundary) ของ bounded context ของเรา ซึ่งเป็นจุดที่ I/O ได้รับอนุญาต แบบนี้:

​  ​ type ​ PayInvoiceCommand = {
​  InvoiceId : ...
​  Payment : ...
​  }
​  
​  ​ // command handler at the edge of the bounded context ​
​  ​ let ​ payInvoice payInvoiceCommand =
​  ​ // load from DB ​
​  ​ let ​ invoiceId = payInvoiceCommand.InvoiceId
​  ​ let ​ unpaidInvoice =
​  loadInvoiceFromDatabase invoiceId ​ // I/O ​
​  
​  ​ // call into pure domain ​
​  ​ let ​ payment =
​  payInvoiceCommand.Payment ​ // pure ​
​  ​ let ​ paymentResult =
​  applyPayment unpaidInvoice payment ​ // pure ​
​  
​  ​ // handle result ​
​  ​ match ​ paymentResult ​ with ​
​  | FullyPaid ->
​  markAsFullyPaidInDb invoiceId ​ // I/O ​
​  postInvoicePaidEvent invoiceId ​ // I/O ​
​  | PartiallyPaid updatedInvoice ->
​  updateInvoiceInDb updatedInvoice ​ // I/O ​

โปรดสังเกตว่า function นี้ไม่ได้ตัดสินใจอะไรด้วยตัวเองเลย มันแค่จัดการกับ InvoicePaymentResult ที่ function ซึ่งเน้น domain เป็นศูนย์กลางด้านในคืนค่ามา ผลก็คือ function นี้ไม่จำเป็นต้องทดสอบด้วย unit test จริงๆ เพราะ persistence logic โดยทั่วไปแล้วค่อนข้างเรียบง่าย นั่นไม่ได้หมายความว่าไม่ควรทดสอบเลยแน่นอน แต่คุณอาจจะทดสอบมันได้ดีกว่าในฐานะส่วนหนึ่งของ end-to-end integration test

คุณสามารถคิดถึง function ผสมนี้เหมือนแซนด์วิช—I/O อยู่ที่ขอบทั้งสองข้าง โดยมีศูนย์กลางที่เป็น pure อยู่ตรงกลาง ดังที่แสดงใน รูปภาพ .

images/io-sandwich1.png

อย่างไรก็ตาม ถ้าเรา ต้องการ ทดสอบ function นี้แบบแยกเดี่ยวจริงๆ สิ่งที่เราต้องทำก็แค่เพิ่มพารามิเตอร์ function เข้าไปเพื่อแทน I/O action ทั้งหมดที่เราเรียกใช้ แบบนี้:

​  ​ // command handler at the edge of the bounded context ​
​  ​ let ​ payInvoice
​  loadUnpaidInvoiceFromDatabase ​ // dependency ​
​  markAsFullyPaidInDb ​ // dependency ​
​  updateInvoiceInDb ​ // dependency ​
​  payInvoiceCommand =
​  
​  ​ // load from DB ​
​  ​ let ​ invoiceId = payInvoiceCommand.InvoiceId
​  ​ let ​ unpaidInvoice =
​  loadUnpaidInvoiceFromDatabase invoiceId
​  
​  ​ // call into pure domain ​
​  ​ let ​ payment =
​  payInvoiceCommand.Payment
​  ​ let ​ paymentResult =
​  applyPayment unpaidInvoice payment
​  
​  ​ // handle result ​
​  ​ match ​ paymentResult ​ with ​
​  | FullyPaid ->
​  markAsFullyPaidInDb(invoiceId)
​  postInvoicePaidEvent(invoiceId)
​  | PartiallyPaid updatedInvoice ->
​  updateInvoiceInDb updatedInvoice

และตอนนี้คุณก็สามารถทดสอบ function นี้ได้ง่ายๆ โดยการใส่ stub ให้กับพารามิเตอร์ตามวิธีปกติ

แน่นอนว่า function ผสมที่ใช้ I/O แบบนี้ควรอยู่ที่ระดับบนสุดของแอปพลิเคชัน—ไม่ว่าจะเป็นใน “composition root” หรือใน controller

Making Decisions Based on Queries (การตัดสินใจโดยอิงจาก Query)

ตัวอย่างด้านบนสมมติว่าข้อมูลทั้งหมดสามารถโหลดจากภายนอก domain function แล้วส่งเข้าไปให้มันได้ แต่จะเกิดอะไรขึ้นถ้าคุณต้องตัดสินใจโดยอิงจากการอ่าน database ตรงกลางของ code ที่เป็น “pure” เลย?

ทางแก้คือให้คง pure function ไว้เหมือนเดิม แต่ประกบมันไว้ระหว่าง function I/O ที่ไม่ pure แบบนี้

images/io-sandwich2.png

pure function จะมี business logic และเป็นตัวตัดสินใจ ส่วน function I/O จะอ่านและเขียนข้อมูล

ตัวอย่างเช่น สมมติว่าเราต้องขยาย workflow: หลังจากชำระเงินแล้ว เราจะคำนวณยอดเงินค้างชำระทั้งหมด แล้วส่ง warning message ไปให้ลูกค้าถ้ายอดนั้นสูงเกินไป เมื่อมี requirement เพิ่มเติม ขั้นตอนใน pipeline ก็จะมีหน้าตาประมาณนี้:

​  --- I/O---
​  Load invoice from DB
​  
​  --- Pure ---
​  Do payment logic
​  
​  --- I/O ---
​  Pattern match on output choice type:
​  if "FullyPaid" -> Mark invoice as paid in DB
​  if "PartiallyPaid" -> Save updated invoice to DB
​  
​  --- I/O ---
​  Load all amounts from unpaid invoices in DB
​  
​  --- Pure ---
​  Add the amounts up and decide if amount is too large
​  
​  --- I/O ---
​  Pattern match on output choice type:
​  If "OverdueWarningNeeded" -> Send message to customer
​  If "NoActionNeeded" -> do nothing

ถ้ามีการผสม I/O กับ logic มากเกินไป แซนด์วิชธรรมดาก็อาจกลายเป็น “เค้กหลายชั้น” แทน ในกรณีนั้น คุณอาจอยากแบ่ง workflow ออกเป็น mini-workflow สั้นๆ ดังที่กล่าวถึงใน ​ Long-Running Workflows ​ วิธีนี้จะทำให้แต่ละ workflow ยังคงเป็นแซนด์วิชเล็กๆ ที่เรียบง่ายอยู่ได้

Where's the Repository Pattern? (แล้ว Repository Pattern ล่ะ?)

ในหนังสือ Domain-Driven Design ต้นฉบับ มี pattern สำหรับเข้าถึง database ที่เรียกว่า Repository pattern ถ้าคุณคุ้นเคยกับหนังสือเล่มนั้น คุณอาจสงสัยว่า pattern นั้นจะเข้ากับแนวทางแบบ functional ได้อย่างไร

คำตอบคือมันไม่เข้ากันเลย Repository pattern เป็นวิธีที่ดีในการซ่อน persistence ไว้ใน object-oriented design ที่พึ่งพา mutability แต่เมื่อเราโมเดลทุกอย่างเป็น function และผลัก persistence ไปไว้ที่ขอบระบบแล้ว Repository pattern ก็ไม่จำเป็นอีกต่อไป

แนวทางนี้ยังมีข้อดีในแง่ maintainability ด้วย เพราะแทนที่จะมี I/O interface เดียวที่มี method นับสิบตัว ซึ่งส่วนใหญ่เราไม่ต้องใช้ใน workflow หนึ่งๆ เลย เรากลับนิยาม function แยกต่างหากสำหรับการเข้าถึง I/O แต่ละอย่างโดยเฉพาะ แล้วใช้มันเท่าที่จำเป็นเท่านั้น