ตอนนี้เราเข้าใจแนวคิดแล้ว มาลองใช้งานจริงกันดีกว่า เราจะ compose workflow pipeline โดยใช้ function ที่สร้าง error ของเรา ปรับแต่งมันตามความจำเป็นเพื่อให้เข้ากันได้
มาทบทวนส่วนประกอบต่างๆ ของ pipeline ของเรากันเร็วๆ โดยโฟกัสไปที่ Result และยังไม่สนใจ effect Async กับ service dependencies ตอนนี้ก่อน
ก่อนอื่น ValidateOrder จะคืน error ออกมาถ้ารูปแบบข้อมูล input ไม่ถูกต้อง มันจึงเป็น function “switch” และ signature ของมันจะเป็นแบบนี้:
| | type ValidateOrder = |
| | // ignoring additional dependencies for now |
| | UnvalidatedOrder // input |
| | -> Result<ValidatedOrder, ValidationError> // output |
Step PriceOrder ก็อาจล้มเหลวได้ด้วยเหตุผลหลากหลาย signature ของมันจึงเป็นแบบนี้:
| | type PriceOrder = |
| | ValidatedOrder // input |
| | -> Result<PricedOrder, PricingError> // output |
ส่วน step AcknowledgeOrder และ CreateEvents จะสำเร็จเสมอ signature ของมันจึงเป็น:
| | type AcknowledgeOrder = |
| | PricedOrder // input |
| | -> OrderAcknowledgmentSent option // output |
| | |
| | type CreateEvents = |
| | PricedOrder // input |
| | -> OrderAcknowledgmentSent option // input (event from previous step) |
| | -> PlaceOrderEvent list // output |
มาเริ่มด้วยการรวม ValidateOrder กับ PriceOrder กันก่อน error type ของ ValidateOrder คือ ValidationError ในขณะที่ error type ของ PriceOrder คือ PricingError อย่างที่เราเห็นข้างต้น function ทั้งสองเข้ากันไม่ได้เพราะ error type ต่างกัน เราจึงต้องแปลงทั้งสอง function ให้คืน error type เดียวกัน —error type ร่วมที่จะใช้ทั่วทั้ง pipeline ซึ่งเราจะเรียกมันว่า PlaceOrderError โดย PlaceOrderError จะถูกนิยามแบบนี้:
| | type PlaceOrderError = |
| | | Validation of ValidationError |
| | | Pricing of PricingError |
ทีนี้เมื่อใช้ mapError เราก็สามารถนิยามเวอร์ชันใหม่ของ validateOrder และ priceOrder ที่ compose กันได้ เหมือนกับที่เราทำในตัวอย่าง FruitError ข้างต้น:
| | // Adapted to return a PlaceOrderError |
| | let validateOrderAdapted input = |
| | input |
| | |> validateOrder // the original function |
| | |> Result.mapError PlaceOrderError.Validation |
| | |
| | // Adapted to return a PlaceOrderError |
| | let priceOrderAdapted input = |
| | input |
| | |> priceOrder // the original function |
| | |> Result.mapError PlaceOrderError.Pricing |
เมื่อทำเสร็จแล้ว ในที่สุดเราก็สามารถเชื่อมมันเข้าด้วยกันได้โดยใช้ bind :
| | let placeOrder unvalidatedOrder = |
| | unvalidatedOrder |
| | |> validateOrderAdapted // adapted version |
| | |> Result.bind priceOrderAdapted // adapted version |
สังเกตว่า function validateOrderAdapted ไม่จำเป็นต้องมี bind นำหน้า เพราะมันเป็นตัวแรกใน pipeline
ต่อไป acknowledgeOrder และ createEvents ไม่มี error เลย—มันเป็น function แบบ “one-track”—เราจึงใช้ Result.map แปลงมันให้เป็น function แบบ two-track ที่เสียบเข้าไปใน pipeline ได้:
| | let placeOrder unvalidatedOrder = |
| | unvalidatedOrder |
| | |> validateOrderAdapted |
| | |> Result.bind priceOrderAdapted |
| | |> Result.map acknowledgeOrder // use map to convert to two-track |
| | |> Result.map createEvents // convert to two-track |
function placeOrder นี้มี signature แบบนี้:
| | UnvalidatedOrder -> Result<PlaceOrderEvent list ,PlaceOrderError> |
ซึ่งใกล้เคียงกับสิ่งที่เราต้องการมากแล้ว
มาวิเคราะห์ workflow pipeline เวอร์ชันใหม่นี้กัน:
-
แต่ละ function ใน pipeline สามารถสร้าง error ได้ และ error ที่มันสร้างได้ก็ถูกระบุไว้ใน signature ของมัน เราสามารถทดสอบแต่ละ function แยกกันได้ โดยมั่นใจว่าเมื่อประกอบเข้าด้วยกันแล้วจะไม่มีพฤติกรรมที่ไม่คาดคิดเกิดขึ้น
-
function ต่างๆ ยังคงถูกเชื่อมเข้าด้วยกัน แต่ตอนนี้ใช้ two-track model: error ในหนึ่ง step จะทำให้ function ที่เหลือใน pipeline ถูกข้ามไปทั้งหมด
-
โฟลว์โดยรวมใน placeOrder ระดับบนสุดยังคงสะอาดอยู่ ไม่มี conditional พิเศษหรือ try/catch block ใดๆ
แต่น่าเสียดายที่การ implement placeOrder นี้จะคอมไพล์ไม่ผ่านจริงๆ! แม้เราจะใช้ bind และ map แล้ว function เหล่านี้ก็ยังไม่เข้ากันเสมอไป โดยเฉพาะ output ของ acknowledgeOrder ไม่ตรงกับ input ของ createEvents เพราะ output เป็นแค่ event เท่านั้น ไม่ใช่ priced order เดี๋ยวเราจะมาดูวิธีจัดการกับปัญหานี้กันในไม่ช้า