ใน บทสนทนาก่อนหน้านี้ เราพูดถึงการบันทึก effect ไว้ใน type signature ว่า function นี้อาจมี effect อะไรได้บ้าง? มันคืนค่า error ได้ไหม? มันทำ I/O หรือเปล่า?

ทีนี้มาทบทวน dependencies ทั้งหมดของเราอย่างรวดเร็ว และตรวจสอบอีกครั้งว่าเราจำเป็นต้องระบุ effect แบบนี้ให้ชัดเจนหรือไม่

Effects in the Validation Step (Effect ในขั้นตอนการ Validate)

ขั้นตอน validation มี dependencies สองตัวคือ CheckProductCodeExists และ CheckAddress Exists

เริ่มด้วย CheckProductCodeExists ก่อน

​  ​ type ​ CheckProductCodeExists = ProductCode -> ​ bool ​

มันคืนค่า error ได้ไหม แล้วมันเป็นการเรียกแบบ remote หรือเปล่า?

สมมติว่ามันไม่ใช่ทั้งสองอย่าง แทนที่จะเป็นแบบนั้น เราจะคาดหวังว่ามีสำเนาของ product catalog ที่ cache ไว้ในเครื่อง (local) อยู่แล้ว (ลองนึกถึงสิ่งที่ Ollie พูดเกี่ยวกับ autonomy ใน Chapter 2, ​ Understanding the Domain ​ ) และเราสามารถเข้าถึงมันได้อย่างรวดเร็ว

ในทางกลับกัน เรารู้ว่า function CheckAddressExists เรียก remote service ไม่ใช่ service ในเครื่องภายใน domain ดังนั้นมันจึงควรมี effect Async ด้วยเช่นเดียวกับ effect Result จริงๆ แล้ว effect Async และ Result มักถูกใช้ร่วมกันบ่อยมาก จนโดยทั่วไปเราจะรวมมันเข้าเป็น type เดียวโดยใช้ alias AsyncResult

​  ​ type ​ AsyncResult<​ ' ​success,​ ' ​failure> = Async<Result<​ ' ​success,​ ' ​failure>>

ด้วยสิ่งนี้ ตอนนี้เราสามารถเปลี่ยน return type ของ CheckAddressExists จาก Result เป็น AsyncResult เพื่อบ่งบอกว่า function นี้มีทั้ง effect แบบ async และแบบ error

​  ​ type ​ CheckAddressExists =
​  UnvalidatedAddress -> AsyncResult<CheckedAddress,AddressValidationError>

ตอนนี้จาก type signature ก็ชัดเจนแล้วว่า function CheckAddressExists กำลังทำ I/O และมันอาจล้มเหลวได้ ก่อนหน้านี้ตอนที่พูดถึง bounded contexts เราบอกว่า autonomy เป็นปัจจัยสำคัญ แล้วนั่นหมายความว่าเราควรพยายามสร้าง address validation service เวอร์ชัน local ของเราเองไหม? ถ้าเรายกเรื่องนี้ขึ้นมา คุยกับ Ollie เราจะได้รับการยืนยันว่า service นี้มี availability สูงมาก จำไว้ว่าเหตุผลหลักที่ต้องการ autonomy ไม่ใช่เรื่อง performance แต่เป็นการทำให้เราสามารถรับประกันระดับ availability และคุณภาพการให้บริการได้ ถ้า implementation ของคุณต้องพึ่งพา third party คุณก็ต้องเชื่อใจพวกเขาจริงๆ (หรือไม่ก็ต้องหาทางแก้ปัญหาเมื่อ service มีปัญหา)

เช่นเดียวกับ Result effect Async ก็แพร่กระจายไปยัง code ทุกส่วนที่มีมันอยู่ ดังนั้นการเปลี่ยน CheckAddressExists ให้คืนค่าเป็น AsyncResult หมายความว่าเราต้องเปลี่ยนขั้นตอน ValidateOrder ทั้งหมดให้คืนค่าเป็น AsyncResult ด้วยเช่นกัน

​  ​ type ​ ValidateOrder =
​  CheckProductCodeExists ​ // dependency ​
​  -> CheckAddressExists ​ // AsyncResult dependency ​
​  -> UnvalidatedOrder ​ // input ​
​  -> AsyncResult<ValidatedOrder,ValidationError ​ list ​> ​ // output ​

Effects in the Pricing Step (Effect ในขั้นตอนการตั้งราคา)

ขั้นตอนการตั้งราคามี dependency แค่ตัวเดียวคือ GetProductPrice เราจะสมมติเหมือนเดิมว่า product catalog อยู่ในเครื่อง (local) (เช่น cache ไว้ในหน่วยความจำ) ดังนั้นจึงไม่มี effect Async และเท่าที่เราเห็น การเข้าถึงมันก็ไม่สามารถคืนค่า error ได้เช่นกัน ดังนั้นจึงไม่มี effect ตรงนี้

อย่างไรก็ตาม ขั้นตอน PriceOrder เองอาจคืนค่า error ได้ สมมติว่าสินค้าถูกตั้งราคาผิด ทำให้ AmountToBill โดยรวมมีค่าสูงมาก (หรือติดลบ) นี่เป็นสิ่งที่เราควรจับได้เมื่อมันเกิดขึ้น มันอาจเป็น edge case ที่แทบไม่เกิดขึ้นเลย แต่ความน่าอับอายในโลกจริงหลายครั้งก็เกิดจาก error แบบนี้แหละ!

ถ้าตอนนี้เราจะคืนค่าเป็น Result เราก็ต้องมี error type คู่กับมันด้วย เราจะเรียกมันว่า PricingError ตอนนี้ function PriceOrder จะมีหน้าตาแบบนี้

​  ​ type ​ PricingError = PricingError ​ of ​ ​ string ​
​  
​  ​ type ​ PriceOrder =
​  GetProductPrice ​ // dependency ​
​  -> ValidatedOrder ​ // input ​
​  -> Result<PricedOrder,PricingError> ​ // output ​

Effects in the Acknowledge Step (Effect ในขั้นตอนการยืนยันคำสั่งซื้อ)

ขั้นตอน AcknowledgeOrder มี dependencies สองตัวคือ CreateOrderAcknowledgmentLetter และ SendOrderAcknowledgment

function CreateOrderAcknowledgmentLetter คืนค่า error ได้ไหม? คงไม่ได้ และเราจะสมมติว่ามันเป็น local และใช้ template ที่ถูก cache ไว้ ดังนั้นโดยรวมแล้ว function CreateOrderAcknowledgmentLetter จึงไม่มี effect ใดๆ ที่จำเป็นต้องบันทึกไว้ใน type signature

ในทางกลับกัน เรารู้ว่า SendOrderAcknowledgment จะต้องทำ I/O ดังนั้นมันจึงต้องมี effect Async แล้ว error ล่ะ? ในกรณีนี้เราไม่สนใจรายละเอียดของ error และถึงแม้จะมี error เราก็ต้องการเพิกเฉยและดำเนินต่อไปตาม happy path ดังนั้นนั่นหมายความว่า SendOrderAcknowledgment เวอร์ชันปรับปรุงจะมี type Async แต่ไม่มี type Result

​  ​ type ​ SendOrderAcknowledgment =
​  OrderAcknowledgment -> Async<SendResult>

แน่นอนว่า effect Async ก็จะกระเพื่อมขึ้นไปยัง function แม่ด้วยเช่นกัน

​  ​ type ​ AcknowledgeOrder =
​  CreateOrderAcknowledgmentLetter ​ // dependency ​
​  -> SendOrderAcknowledgment ​ // Async dependency ​
​  -> PricedOrder ​ // input ​
​  -> Async<OrderAcknowledgmentSent option> ​ // Async output ​