ตอนนี้เราได้เห็นวิธี implement validateOrder แล้ว เราสามารถใช้เทคนิคเดียวกันในการสร้าง function ที่เหลือของ pipeline ได้
นี่คือดีไซน์ดั้งเดิมของ function ขั้นตอนการคิดราคา พร้อม effect:
| | type PriceOrder = |
| | GetProductPrice // dependency |
| | -> ValidatedOrder // input |
| | -> Result<PricedOrder, PlaceOrderError> // output |
แต่เช่นเดิม เราจะตัด effect ออกไปก่อน เหลือดีไซน์นี้:
| | type GetProductPrice = ProductCode -> Price |
| | type PriceOrder = |
| | GetProductPrice // dependency |
| | -> ValidatedOrder // input |
| | -> PricedOrder // output |
และนี่คือโครงร่างของ implementation มันแค่แปลงแต่ละ order line ให้เป็น PricedOrderLine แล้วสร้าง PricedOrder ใหม่จากสิ่งเหล่านั้น:
| | let priceOrder : PriceOrder = |
| | fun getProductPrice validatedOrder -> |
| | let lines = |
| | validatedOrder.Lines |
| | |> List.map (toPricedOrderLine getProductPrice) |
| | let amountToBill = |
| | lines |
| | // get each line price |
| | |> List.map ( fun line -> line.LinePrice) |
| | // add them together as a BillingAmount |
| | |> BillingAmount.sumPrices |
| | let pricedOrder : PricedOrder = { |
| | OrderId = validatedOrder.OrderId |
| | CustomerInfo = validatedOrder.CustomerInfo |
| | ShippingAddress = validatedOrder.ShippingAddress |
| | BillingAddress = validatedOrder.BillingAddress |
| | Lines = lines |
| | AmountToBill = amountToBill |
| | } |
| | pricedOrder |
อีกอย่าง ถ้าคุณมีขั้นตอนหลายอันใน pipeline และยังไม่อยาก implement ตอนนี้ (หรือยังไม่รู้ว่าจะ implement ยังไง) คุณสามารถแค่ทำให้มัน fail ด้วย message “not implemented” ได้ แบบนี้:
| | let priceOrder : PriceOrder = |
| | fun getProductPrice validatedOrder -> |
| | failwith "not implemented" |
การใช้ exception แบบ “not implemented” สะดวกมากตอนที่กำลังร่าง implementation คร่าวๆ มันช่วยให้เรามั่นใจได้ว่าโปรเจกต์ของเรา compile ผ่านได้ตลอดเวลา ตัวอย่างเช่น เราสามารถใช้แนวทางนี้เพื่อสร้าง pipeline stage เวอร์ชัน dummy ที่สอดคล้องกับ function type แล้วนำไปใช้ร่วมกับ stage อื่นๆ ก่อนที่ implementation จริงจะพร้อม
กลับมาที่ implementation ของ priceOrder เราได้แนะนำ helper function ใหม่สองตัวคือ toPricedOrderLine และ BillingAmount.sumPrices .
เราได้เพิ่ม function BillingAmount.sumPrices เข้าไปใน module ที่ใช้ร่วมกันคือ BillingAmount (พร้อมกับ create และ value ) มันแค่บวก list ของ Prices เข้าด้วยกัน แล้ว wrap เป็น BillingAmount ทำไมเราจึงนิยาม type BillingAmount ขึ้นมาตั้งแต่แรก เพราะมันแยกออกจาก Price และกฎ validation อาจแตกต่างกัน
| | /// Sum a list of prices to make a billing amount |
| | /// Raise exception if total is out of bounds |
| | let sumPrices prices = |
| | let total = prices |> List.map Price.value |> List.sum |
| | create total |
function toPricedOrderLine คล้ายกับที่เราเคยเห็นมาก่อน มันเป็น helper function ที่แปลง line เดียวเท่านั้น:
| | /// Transform a ValidatedOrderLine to a PricedOrderLine |
| | let toPricedOrderLine getProductPrice (line:ValidatedOrderLine) : PricedOrderLine = |
| | let qty = line.Quantity |> OrderQuantity.value |
| | let price = line.ProductCode |> getProductPrice |
| | let linePrice = price |> Price.multiply qty |
| | { |
| | OrderLineId = line.OrderLineId |
| | ProductCode = line.ProductCode |
| | Quantity = line.Quantity |
| | LinePrice = linePrice |
| | } |
และภายใน function นี้ เราได้แนะนำ helper function อีกตัวคือ Price.multiply เพื่อคูณ Price ด้วยจำนวน
| | /// Multiply a Price by a decimal qty. |
| | /// Raise exception if new price is out of bounds. |
| | let multiply qty (Price p) = |
| | create (qty * p) |
ขั้นตอนการคิดราคาเสร็จสมบูรณ์แล้ว!
Implementing the Acknowledgment Step (การ Implement ขั้นตอน Acknowledgment)
นี่คือดีไซน์ของขั้นตอน acknowledgment โดยตัด effect ออกไปแล้ว:
| | type HtmlString = HtmlString of string |
| | type CreateOrderAcknowledgmentLetter = |
| | PricedOrder -> HtmlString |
| | |
| | type OrderAcknowledgment = { |
| | EmailAddress : EmailAddress |
| | Letter : HtmlString |
| | } |
| | type SendResult = Sent | NotSent |
| | type SendOrderAcknowledgment = |
| | OrderAcknowledgment -> SendResult |
| | |
| | type AcknowledgeOrder = |
| | CreateOrderAcknowledgmentLetter // dependency |
| | -> SendOrderAcknowledgment // dependency |
| | -> PricedOrder // input |
| | -> OrderAcknowledgmentSent option // output |
และนี่คือ implementation:
| | let acknowledgeOrder : AcknowledgeOrder = |
| | fun createAcknowledgmentLetter sendAcknowledgment pricedOrder -> |
| | let letter = createAcknowledgmentLetter pricedOrder |
| | let acknowledgment = { |
| | EmailAddress = pricedOrder.CustomerInfo.EmailAddress |
| | Letter = letter |
| | } |
| | |
| | // if the acknowledgment was successfully sent, |
| | // return the corresponding event, else return None |
| | match sendAcknowledgment acknowledgment with |
| | | Sent -> |
| | let event = { |
| | OrderId = pricedOrder.OrderId |
| | EmailAddress = pricedOrder.CustomerInfo.EmailAddress |
| | } |
| | Some event |
| | | NotSent -> |
| | None |
implementation นี้ตรงไปตรงมา ไม่ต้องใช้ helper function เลย ง่ายมาก!
แล้ว dependency sendAcknowledgment ล่ะ ในบางจุดเราต้องตัดสินใจเลือก implementation สำหรับมัน แต่ตอนนี้เราปล่อยมันไว้ก่อนได้ นี่คือหนึ่งในข้อดีที่ยอดเยี่ยมของการใช้ function เพื่อ parameterize dependencies—คุณสามารถเลื่อนการตัดสินใจออกไปจนถึงจุดสุดท้ายที่ยังรับผิดชอบได้ แต่ก็ยังสร้างและประกอบ code ส่วนใหญ่ได้อยู่ดี
Creating the Events (การสร้าง Event)
สุดท้าย เราแค่ต้องสร้าง event ที่จะคืนกลับจาก workflow มาเพิ่มความซับซ้อนเล็กน้อยให้ requirement แล้วบอกว่า billing event ควรถูกส่งก็ต่อเมื่อยอดที่ต้องเรียกเก็บมากกว่าศูนย์เท่านั้น นี่คือดีไซน์:
| | /// Event to send to shipping context |
| | type OrderPlaced = PricedOrder |
| | |
| | /// Event to send to billing context |
| | /// Will only be created if the AmountToBill is not zero |
| | type BillableOrderPlaced = { |
| | OrderId : OrderId |
| | BillingAddress: Address |
| | AmountToBill : BillingAmount |
| | } |
| | |
| | type PlaceOrderEvent = |
| | | OrderPlaced of OrderPlaced |
| | | BillableOrderPlaced of BillableOrderPlaced |
| | | AcknowledgmentSent of OrderAcknowledgmentSent |
| | |
| | type CreateEvents = |
| | PricedOrder // input |
| | -> OrderAcknowledgmentSent option // input (event from previous step) |
| | -> PlaceOrderEvent list // output |
เราไม่จำเป็นต้องสร้าง event OrderPlaced เพราะมันเหมือนกับ event PricedOrder เลย และ event OrderAcknowledgmentSent ก็ถูกสร้างไว้แล้วในขั้นตอนก่อนหน้า ดังนั้นเราก็ไม่ต้องสร้างมันเช่นกัน
แต่ event BillableOrderPlaced จำเป็นต้องมี ดังนั้นมาสร้าง function createBillingEvent กัน และเนื่องจากเราต้องตรวจสอบว่ายอด billing ไม่เท่ากับศูนย์ function นี้จึงต้องคืนค่าเป็น event แบบ optional .
| | // PricedOrder -> BillableOrderPlaced option |
| | let createBillingEvent (placedOrder:PricedOrder) : BillableOrderPlaced option = |
| | let billingAmount = placedOrder.AmountToBill |> BillingAmount.value |
| | if billingAmount > 0M then |
| | let order = { |
| | OrderId = placedOrder.OrderId |
| | BillingAddress = placedOrder.BillingAddress |
| | AmountToBill = placedOrder.AmountToBill |
| | } |
| | Some order |
| | else |
| | None |
ตอนนี้เรามี event OrderPlaced , event OrderAcknowledgmentSent แบบ optional, และ BillableOrderPlaced แบบ optional เราจะคืนค่าพวกมันยังไงดี เราจะใช้แนวทาง “ตัวคูณร่วมน้อยที่สุด” (lowest common multiple) เพื่อแปลงทุกอย่างให้เป็น type ร่วมกัน
เราตัดสินใจไว้ก่อนหน้านี้แล้วว่าจะสร้าง choice type ( PlaceOrderEvent ) สำหรับแต่ละอัน แล้วคืนค่าเป็น list ของสิ่งเหล่านั้น ดังนั้นอย่างแรกเราต้องแปลงแต่ละ event ให้เป็น choice type สำหรับ event OrderPlaced เราแค่ใช้ constructor PlaceOrderEvent.OrderPlaced ได้โดยตรง แต่สำหรับ OrderAcknowledgmentSent และ BillableOrderPlaced เราต้องใช้ Option.map เพราะพวกมันเป็น optional
| | let createEvents : CreateEvents = |
| | fun pricedOrder acknowledgmentEventOpt -> |
| | let event1 = |
| | pricedOrder |
| | // convert to common choice type |
| | |> PlaceOrderEvent.OrderPlaced |
| | let event2Opt = |
| | acknowledgmentEventOpt |
| | // convert to common choice type |
| | |> Option.map PlaceOrderEvent.AcknowledgmentSent |
| | let event3Opt = |
| | pricedOrder |
| | |> createBillingEvent |
| | // convert to common choice type |
| | |> Option.map PlaceOrderEvent.BillableOrderPlaced |
| | |
| | // return all the events how? |
| | ... |
ตอนนี้ทุกอันเป็น type เดียวกันแล้ว แต่บางอันเป็น optional เราควรจัดการกับสิ่งนี้ยังไง เราจะใช้เทคนิคเดิมอีกครั้งและแปลงทั้งหมดให้เป็น type ทั่วไปมากขึ้น—ในกรณีนี้คือ list
สำหรับ OrderPlaced เราแปลงมันเป็น list ได้โดยใช้ List.singleton และสำหรับ option เราสามารถสร้าง helper ที่ชื่อ listOfOption ได้:
| | /// convert an Option into a List |
| | let listOfOption opt = |
| | match opt with |
| | | Some x -> [x] |
| | | None -> [] |
เมื่อทำแบบนั้นแล้ว event ทั้งสาม type จะเหมือนกัน และเราสามารถคืนค่าพวกมันไว้ใน list อีกอันได้:
| | let createEvents : CreateEvents = |
| | fun pricedOrder acknowledgmentEventOpt -> |
| | let events1 = |
| | pricedOrder |
| | // convert to common choice type |
| | |> PlaceOrderEvent.OrderPlaced |
| | // convert to list |
| | |> List.singleton |
| | let events2 = |
| | acknowledgmentEventOpt |
| | // convert to common choice type |
| | |> Option.map PlaceOrderEvent.AcknowledgmentSent |
| | // convert to list |
| | |> listOfOption |
| | let events3 = |
| | pricedOrder |
| | |> createBillingEvent |
| | // convert to common choice type |
| | |> Option.map PlaceOrderEvent.BillableOrderPlaced |
| | // convert to list |
| | |> listOfOption |
| | |
| | // return all the events |
| | [ |
| | yield ! events1 |
| | yield ! events2 |
| | yield ! events3 |
| | ] |
แนวทางการแปลงหรือ “lifting” สิ่งที่ไม่เข้ากันให้เป็น type ที่ใช้ร่วมกันนี้เป็นเทคนิคสำคัญในการจัดการปัญหาเรื่อง composition ตัวอย่างเช่น ในบทถัดไปเราจะใช้มันเพื่อจัดการกับความไม่ตรงกันระหว่าง Result type ชนิดต่างๆ