เราได้เห็น code กระจัดกระจายเป็นชิ้นๆ ตลอดทั้งบทนี้ มารวบรวมทุกอย่างเข้าด้วยกันและแสดงให้เห็นว่า pipeline ที่สมบูรณ์จะถูกประกอบขึ้นมายังไง
- เราจะใส่ code ทั้งหมดที่ implement workflow หนึ่งๆ ไว้ใน module เดียวกัน โดยตั้งชื่อตาม workflow นั้น ( PlaceOrderWorkflow.fs ตัวอย่างเช่น)
- ที่ด้านบนสุดของไฟล์ เราจะใส่ type definition
- ต่อจากนั้น เราจะใส่ implementation ของแต่ละขั้นตอน
- ที่ด้านล่างสุด เราจะประกอบขั้นตอนต่างๆ เข้าเป็น workflow function หลัก
เพื่อประหยัดพื้นที่ เราจะแสดงแค่โครงร่างของเนื้อหาไฟล์เท่านั้น ถ้าคุณอยากดูไฟล์ฉบับเต็ม โค้ดทั้งหมดสำหรับบทนี้มีอยู่ใน code repository ที่มาพร้อมกับหนังสือเล่มนี้
อย่างแรก มาดู type กันก่อน:
ตอนนี้ type ของ workflow ที่เป็น “public” (ส่วนหนึ่งของ contract กับผู้เรียก workflow) จะถูกนิยามไว้ที่อื่น อาจจะอยู่ใน module API ดังนั้นเราแค่ต้องรวม type ในไฟล์นี้ (ที่ออกแบบไว้ใน Chapter 7, Modeling Workflows as Pipelines ) ที่เป็นตัวแทนของขั้นตอนภายในของ workflow เท่านั้น
| | module PlaceOrderWorkflow = |
| | |
| | // make the shared simple types (such as |
| | // String50 and ProductCode) available. |
| | open SimpleTypes |
| | |
| | // make the public types exposed to the |
| | // callers available |
| | open API |
| | |
| | // ============================== |
| | // Part 1: Design |
| | // ============================== |
| | |
| | // NOTE: the public parts of the workflow -- the API -- |
| | // such as the `PlaceOrderWorkflow` function and its |
| | // input `UnvalidatedOrder`, are defined elsewhere. |
| | // The types below are private to the workflow implementation. |
| | |
| | // ----- Validate Order ----- |
| | |
| | type CheckProductCodeExists = |
| | ProductCode -> bool |
| | type CheckedAddress = |
| | CheckedAddress of UnvalidatedAddress |
| | type CheckAddressExists = |
| | UnvalidatedAddress -> CheckedAddress |
| | type ValidateOrder = |
| | CheckProductCodeExists // dependency |
| | -> CheckAddressExists // dependency |
| | -> UnvalidatedOrder // input |
| | -> ValidatedOrder // output |
| | |
| | |
| | // ----- Price order ----- |
| | |
| | type GetProductPrice = ... |
| | type PriceOrder = ... |
| | // etc |
หลังจากส่วนของ type ในไฟล์เดียวกัน เราสามารถใส่ implementation ที่ใช้ type เหล่านั้นได้ นี่คือขั้นตอนแรก validateOrder สรุปมาจาก implementation ก่อนหน้านี้ในบทนี้ .
| | // ============================== |
| | // Part 2: Implementation |
| | // ============================== |
| | |
| | // ------------------------------ |
| | // ValidateOrder implementation |
| | // ------------------------------ |
| | |
| | let toCustomerInfo (unvalidatedCustomerInfo: UnvalidatedCustomerInfo) = |
| | ... |
| | |
| | let toAddress (checkAddressExists:CheckAddressExists) unvalidatedAddress = |
| | ... |
| | |
| | let predicateToPassthru = ... |
| | |
| | let toProductCode (checkProductCodeExists:CheckProductCodeExists) productCode = |
| | ... |
| | |
| | let toOrderQuantity productCode quantity = |
| | ... |
| | |
| | let toValidatedOrderLine checkProductExists (unvalidatedOrderLine:UnvalidatedOrderLine) = |
| | ... |
| | |
| | /// Implementation of ValidateOrder step |
| | let validateOrder : ValidateOrder = |
| | fun checkProductCodeExists checkAddressExists unvalidatedOrder -> |
| | let orderId = |
| | unvalidatedOrder.OrderId |
| | |> OrderId.create |
| | let customerInfo = ... |
| | let shippingAddress = ... |
| | let billingAddress = ... |
| | let lines = |
| | unvalidatedOrder.Lines |
| | |> List.map (toValidatedOrderLine checkProductCodeExists) |
| | let validatedOrder : ValidatedOrder = { |
| | OrderId = orderId |
| | CustomerInfo = customerInfo |
| | ShippingAddress = shippingAddress |
| | BillingAddress = billingAddress |
| | Lines = lines |
| | } |
| | validatedOrder |
เราจะข้าม implementation ของขั้นตอนที่เหลือไป (อีกครั้ง คุณสามารถดู code ทั้งหมดได้ใน repository ที่มาพร้อมกับหนังสือเล่มนี้) แล้วข้ามไปที่ด้านล่างสุดของไฟล์ ที่ซึ่ง function ระดับบนสุด PlaceOrder ถูก implement:
| | // ------------------------------ |
| | // The complete workflow |
| | // ------------------------------ |
| | let placeOrder |
| | checkProductExists // dependency |
| | checkAddressExists // dependency |
| | getProductPrice // dependency |
| | createOrderAcknowledgmentLetter // dependency |
| | sendOrderAcknowledgment // dependency |
| | : PlaceOrderWorkflow = // definition of function |
| | |
| | fun unvalidatedOrder -> |
| | let validatedOrder = |
| | unvalidatedOrder |
| | |> validateOrder checkProductExists checkAddressExists |
| | let pricedOrder = |
| | validatedOrder |
| | |> priceOrder getProductPrice |
| | let acknowledgmentOption = |
| | pricedOrder |
| | |> acknowledgeOrder createOrderAcknowledgmentLetter sendOrderAcknowledgment |
| | let events = |
| | createEvents pricedOrder acknowledgmentOption |
| | events |