ตอนนี้เราเริ่ม implement ขั้นตอน validation ได้แล้ว ขั้นตอน validation จะรับ unvalidated order ที่มี field แบบ primitive ทั้งหมด แล้วแปลงมันให้เป็น domain object ที่ผ่านการ validate อย่างสมบูรณ์
เราโมเดล function types สำหรับขั้นตอนนี้ไว้แบบนี้:
| | type CheckAddressExists = |
| | UnvalidatedAddress -> AsyncResult<CheckedAddress,AddressValidationError> |
| | |
| | type ValidateOrder = |
| | CheckProductCodeExists // dependency |
| | -> CheckAddressExists // AsyncResult dependency |
| | -> UnvalidatedOrder // input |
| | -> AsyncResult<ValidatedOrder,ValidationError list > // output |
อย่างที่เราบอกไป เราจะตัด effect ออกไปก่อนสำหรับบทนี้ ดังนั้นเราจึงเอาส่วน AsyncResult ออก เหลือแค่นี้:
| | type CheckAddressExists = |
| | UnvalidatedAddress -> CheckedAddress |
| | |
| | type ValidateOrder = |
| | CheckProductCodeExists // dependency |
| | -> CheckAddressExists // dependency |
| | -> UnvalidatedOrder // input |
| | -> ValidatedOrder // output |
มาแปลงสิ่งนี้ให้เป็น implementation กัน ขั้นตอนในการสร้าง ValidatedOrder จาก UnvalidatedOrder จะเป็นดังนี้:
-
สร้าง domain type OrderId จาก string OrderId ที่สอดคล้องกันใน unvalidated order
-
สร้าง domain type CustomerInfo จาก field UnvalidatedCustomerInfo ที่สอดคล้องกันใน unvalidated order
-
สร้าง domain type Address จาก field ShippingAddress ที่สอดคล้องกันใน unvalidated order ซึ่งเป็น UnvalidatedAddress .
-
ทำแบบเดียวกันกับ BillingAddress และ property อื่นๆ ทั้งหมด
-
เมื่อเรามีทุก component ของ ValidatedOrder พร้อมแล้ว เราก็สามารถสร้าง record ได้ตามปกติ
นี่คือหน้าตาของ code:
| | let validateOrder : ValidateOrder = |
| | fun checkProductCodeExists checkAddressExists unvalidatedOrder -> |
| | |
| | let orderId = |
| | unvalidatedOrder.OrderId |
| | |> OrderId.create |
| | |
| | let customerInfo = |
| | unvalidatedOrder.CustomerInfo |
| | |> toCustomerInfo // helper function |
| | |
| | let shippingAddress = |
| | unvalidatedOrder.ShippingAddress |
| | |> toAddress // helper function |
| | |
| | // and so on, for each property of the unvalidatedOrder |
| | |
| | // when all the fields are ready, use them to |
| | // create and return a new "ValidatedOrder" record |
| | { |
| | OrderId = orderId |
| | CustomerInfo = customerInfo |
| | ShippingAddress = shippingAddress |
| | BillingAddress = ... |
| | Lines = ... |
| | } |
คุณจะเห็นว่าเราใช้ helper function บางตัว เช่น toCustomerInfo และ toAddress ซึ่งเรายังไม่ได้นิยาม function เหล่านี้จะสร้าง domain type จาก unvalidated type ตัวอย่างเช่น toAddress จะแปลง type UnvalidatedAddress เป็น domain type Address ที่สอดคล้องกัน และจะคืน error ถ้าค่า primitive ใน unvalidated address ไม่ตรงตามเงื่อนไข (เช่น ต้องไม่เป็น null และยาวไม่เกิน 50 ตัวอักษร)
เมื่อเรามี helper function เหล่านี้พร้อมแล้ว logic ในการแปลง unvalidated order (หรือ non-domain type ใดๆ) ให้เป็น domain type ก็ตรงไปตรงมา: สำหรับแต่ละ field ของ domain type (ในกรณีนี้คือ ValidatedOrder ) หา field ที่สอดคล้องกันใน non-domain type ( UnvalidatedOrder ) แล้วใช้ helper function ตัวใดตัวหนึ่งเพื่อแปลง field นั้นให้เป็น domain type
เราสามารถใช้แนวทางเดียวกันนี้ในการแปลง subcomponent ของ order ได้เช่นกัน ตัวอย่างเช่น นี่คือ implementation ของ toCustomerInfo ซึ่งสร้าง CustomerInfo จาก UnvalidatedCustomerInfo :
| | let toCustomerInfo (customer:UnvalidatedCustomerInfo) : CustomerInfo = |
| | // create the various CustomerInfo properties |
| | // and throw exceptions if invalid |
| | let firstName = customer.FirstName |> String50.create |
| | let lastName = customer.LastName |> String50.create |
| | let emailAddress = customer.EmailAddress |> EmailAddress.create |
| | |
| | // create a PersonalName |
| | let name : PersonalName = { |
| | FirstName = firstName |
| | LastName = lastName |
| | } |
| | |
| | // create a CustomerInfo |
| | let customerInfo : CustomerInfo = { |
| | Name = name |
| | EmailAddress = emailAddress |
| | } |
| | // ... and return it |
| | customerInfo |
Creating a Valid, Checked Address (การสร้าง Address ที่ถูกต้องและผ่านการตรวจสอบ)
function toAddress ซับซ้อนกว่าเล็กน้อย เพราะมันไม่ได้แค่แปลง raw primitive types ให้เป็น domain object เท่านั้น แต่ยังต้องตรวจสอบว่า address นั้นมีอยู่จริงด้วย (โดยใช้ service CheckAddressExists ) นี่คือ implementation แบบสมบูรณ์ พร้อมคำอธิบายด้านล่าง:
| | let toAddress (checkAddressExists:CheckAddressExists) unvalidatedAddress = |
| | // call the remote service |
| | let checkedAddress = checkAddressExists unvalidatedAddress |
| | // extract the inner value using pattern matching |
| | let (CheckedAddress checkedAddress) = checkedAddress |
| | |
| | let addressLine1 = |
| | checkedAddress.AddressLine1 |> String50.create |
| | let addressLine2 = |
| | checkedAddress.AddressLine2 |> String50.createOption |
| | let addressLine3 = |
| | checkedAddress.AddressLine3 |> String50.createOption |
| | let addressLine4 = |
| | checkedAddress.AddressLine4 |> String50.createOption |
| | let city = |
| | checkedAddress.City |> String50.create |
| | let zipCode = |
| | checkedAddress.ZipCode |> ZipCode.create |
| | // create the address |
| | let address : Address = { |
| | AddressLine1 = addressLine1 |
| | AddressLine2 = addressLine2 |
| | AddressLine3 = addressLine3 |
| | AddressLine4 = addressLine4 |
| | City = city |
| | ZipCode = zipCode |
| | } |
| | // return the address |
| | address |
สังเกตว่าเราอ้างอิงถึง constructor function อีกตัวใน module String50 — String50.createOption —ที่ยอมให้ input เป็น null หรือ empty ได้ และจะคืนค่า None ในกรณีนั้น
function toAddress ต้องเรียก checkAddressExists ดังนั้นเราจึงเพิ่มมันเป็น parameter แล้วส่ง function นั้นมาจาก function หลักคือ validateOrder :
| | let validateOrder : ValidateOrder = |
| | fun checkProductCodeExists checkAddressExists unvalidatedOrder -> |
| | |
| | let orderId = ... |
| | let customerInfo = ... |
| | let shippingAddress = |
| | unvalidatedOrder.ShippingAddress |
| | |> toAddress checkAddressExists // new parameter |
| | |
| | ... |
คุณอาจสงสัยว่าทำไมเราจึงส่งแค่ หนึ่ง parameter ให้ toAddress ทั้งที่จริงๆ มันมีสอง parameter ตัวที่สอง (shipping address) ถูกส่งเข้าไปผ่านกระบวนการ pipe นี่คือตัวอย่างของเทคนิค partial application ที่เรา คุยกันไปก่อนหน้านี้ .
Creating the Order Lines (การสร้าง Order Line)
การสร้าง list ของ order line ยิ่งซับซ้อนกว่านั้นอีก อย่างแรก เราต้องมีวิธีแปลง UnvalidatedOrderLine ตัวเดียวให้เป็น ValidatedOrderLine มาเรียกมันว่า toValidatedOrderLine :
| | let toValidatedOrderLine checkProductCodeExists |
| | (unvalidatedOrderLine:UnvalidatedOrderLine) = |
| | let orderLineId = |
| | unvalidatedOrderLine.OrderLineId |
| | |> OrderLineId.create |
| | let productCode = |
| | unvalidatedOrderLine.ProductCode |
| | |> toProductCode checkProductCodeExists // helper function |
| | let quantity = |
| | unvalidatedOrderLine.Quantity |
| | |> toOrderQuantity productCode // helper function |
| | let validatedOrderLine = { |
| | OrderLineId = orderLineId |
| | ProductCode = productCode |
| | Quantity = quantity |
| | } |
| | validatedOrderLine |
นี่คล้ายกับ function toAddress ด้านบน มี helper function สองตัวคือ toProductCode และ toOrderQuantity ซึ่งเราจะพูดถึงในไม่ช้า
และตอนนี้ เนื่องจากเรามีวิธีแปลงแต่ละ element ใน list แล้ว เราสามารถแปลงทั้ง list ได้ในครั้งเดียวโดยใช้ List.map (เทียบเท่ากับ Select ใน C# LINQ) ทำให้ได้ list ของ ValidatedOrderLines ที่เราสามารถใช้ใน ValidatedOrder ได้:
| | let validateOrder : ValidateOrder = |
| | fun checkProductCodeExists checkAddressExists unvalidatedOrder -> |
| | |
| | let orderId = ... |
| | let customerInfo = ... |
| | let shippingAddress = ... |
| | |
| | let orderLines = |
| | unvalidatedOrder.Lines |
| | // convert each line using `toValidatedOrderLine` |
| | |> List.map (toValidatedOrderLine checkProductCodeExists) |
| | ... |
มาดู helper function toOrderQuantity กันต่อ นี่เป็นตัวอย่างที่ดีของการ validation ตรง boundary ของ bounded context: input คือ decimal ดิบที่ยังไม่ผ่าน validation จาก UnvalidatedOrderLine แต่ output ( OrderQuantity ) เป็น choice type ที่มี validation ต่างกันในแต่ละ case นี่คือ code:
| | let toOrderQuantity productCode quantity = |
| | match productCode with |
| | | Widget _ -> |
| | quantity |
| | |> int // convert decimal to int |
| | |> UnitQuantity.create // to UnitQuantity |
| | |> OrderQuantity.Unit // lift to OrderQuantity type |
| | | Gizmo _ -> |
| | quantity |
| | |> KilogramQuantity.create // to KilogramQuantity |
| | |> OrderQuantity.Kilogram // lift to OrderQuantity type |
เราใช้ case ของ choice type ProductCode เพื่อนำทาง constructor ตัวอย่างเช่น ถ้า ProductCode เป็น Widget เราจะแปลง decimal ดิบให้เป็น int แล้วสร้าง UnitQuantity จากนั้น และทำแบบเดียวกันสำหรับ case GizmoCode .
แต่เราหยุดแค่นั้นไม่ได้ ถ้าเราหยุด branch หนึ่งจะคืน UnitQuantity ส่วนอีก branch จะคืน KilogramQuantity ซึ่งเป็นคนละ type กัน ดังนั้นเราจะได้ compiler error การแปลงทั้งสอง branch ให้เป็น choice type OrderQuantity ทำให้ทั้งสอง branch คืนค่า type เดียวกัน และทำให้คอมไพเลอร์พอใจ!
helper function อีกตัวคือ toProductCode เมื่อมองผ่านๆ น่าจะ implement ได้ง่าย เราอยากเขียน function ของเราโดยใช้ pipeline ให้มากที่สุดเท่าที่จะทำได้ ดังนั้น code ควรมีหน้าตาประมาณนี้:
| | let toProductCode (checkProductCodeExists:CheckProductCodeExists) productCode = |
| | productCode |
| | |> ProductCode.create |
| | |> checkProductCodeExists |
| | // returns a bool :( |
แต่ตอนนี้เรามีปัญหา เราต้องการให้ function toProductCode คืนค่าเป็น ProductCode แต่ function checkProductCodeExists คืนค่าเป็น bool ซึ่งหมายความว่า pipeline ทั้งหมดจะคืนค่าเป็น bool ดังนั้นเราจึงต้องทำให้ checkProductCodeExists คืนค่าเป็น ProductCode แทน โอ๊ะ นั่นหมายความว่าเราต้องเปลี่ยน spec ใช่ไหม โชคดีที่ไม่ต้อง มาดูกันว่าทำยังไง
Creating Function Adapters (การสร้าง Function Adapter)
เรามี function ที่คืนค่าเป็น bool แต่เราอยากได้ function ที่คืนค่า ProductCode ตัวเดิมที่เป็น input (ถ้าทุกอย่างเรียบร้อยดี) แทนที่จะเปลี่ยน spec เรามาสร้าง function “adapter” ที่รับ function เดิมเป็น input แล้วสร้าง function ใหม่ที่มี “รูปร่าง” ถูกต้องสำหรับใช้ใน pipeline
นี่คือ implementation ที่เห็นได้ชัด โดยมี parameter สำหรับ predicate ที่คืนค่า bool ( checkProductCodeExists ) และค่าที่ต้องตรวจสอบ ( productCode ):
| | let convertToPassthru checkProductCodeExists productCode = |
| | if checkProductCodeExists productCode then |
| | productCode |
| | else |
| | failwith "Invalid Product Code" |
สิ่งที่น่าสนใจของ implementation นี้คือ compiler สรุปว่ามันเป็น generic โดยสมบูรณ์—มันไม่ได้เจาะจงกับกรณีของเราเลย! ถ้าคุณดู function signature คุณจะเห็นว่าไม่มีการกล่าวถึง type ProductCode เลย:
| | val convertToPassthru : |
| | checkProductCodeExists:( ' a -> bool ) -> productCode: ' a -> ' a |
จริงๆ แล้วเราสร้าง adapter แบบ generic ขึ้นมาโดยบังเอิญ ที่จะแปลง predicate function ใดก็ได้ ให้เป็น function แบบ “passthrough” ที่เหมาะสำหรับ pipeline การเรียก parameter ว่า “checkProductCodeExists” หรือ “productCode” ตอนนี้ไม่สมเหตุสมผลแล้ว—มันจะเป็นอะไรก็ได้ นี่คือเหตุผลที่ standard library function จำนวนมากใช้ชื่อ parameter สั้นๆ เช่น f และ g สำหรับ parameter ที่เป็น function และ x กับ y สำหรับค่าอื่นๆ
มาเขียน function ใหม่โดยใช้ชื่อที่เป็นนามธรรมมากขึ้นกัน แบบนี้:
| | let predicateToPassthru f x = |
| | if f x then |
| | x |
| | else |
| | failwith "Invalid Product Code" |
และตอนนี้ error message ที่ hard-code ไว้ก็โดดออกมาให้เห็น มา parameterize มันด้วยกัน นี่คือเวอร์ชันสุดท้าย:
| | let predicateToPassthru errorMsg f x = |
| | if f x then |
| | x |
| | else |
| | failwith errorMsg |
สังเกตว่าเราวาง error message ไว้เป็นตัว แรก ใน parameter order เพื่อที่เราจะได้ bake มันเข้าไปด้วย partial application
signature ของ function นี้คือแบบนี้:
| | val predicateToPassthru : errorMsg: string -> f:( ' a -> bool ) -> x: ' a -> ' a |
เราสามารถตีความสิ่งนี้ได้ว่า “คุณให้ error message กับ function type ’a -> bool มา แล้วผมจะให้ function type ’a -> ’a กลับไป” function predicateToPassthru นี้จึงเป็น “function transformer”—คุณป้อน function หนึ่งเข้าไป แล้วมันจะแปลงเป็น function อีกตัว
เทคนิคนี้พบได้ทั่วไปมากใน functional programming ดังนั้นการเข้าใจว่ามันทำงานยังไงและจดจำ pattern นี้เมื่อเห็นจึงสำคัญ แม้แต่ function ธรรมดาอย่าง List.map ก็สามารถมองได้ว่าเป็น function transformer—มันแปลง function “ปกติ” ’a -> ’b ให้เป็น function ที่ทำงานกับ list ( ’a list -> ’b list )
เอาล่ะ ตอนนี้มาใช้ generic function นี้เพื่อสร้าง toProductCode เวอร์ชันใหม่ที่เราสามารถใช้ใน implementation ของเราได้:
| | let toProductCode (checkProductCodeExists:CheckProductCodeExists) productCode = |
| | |
| | // create a local ProductCode -> ProductCode function |
| | // suitable for using in a pipeline |
| | let checkProduct productCode = |
| | let errorMsg = sprintf "Invalid: %A" productCode |
| | predicateToPassthru errorMsg checkProductCodeExists productCode |
| | |
| | // assemble the pipeline |
| | productCode |
| | |> ProductCode.create |
| | |> checkProduct |
แค่นี้เอง—ตอนนี้เรามีโครงร่างพื้นฐานของ implementation validateOrder ที่เราสามารถต่อยอดได้แล้ว สังเกตว่า logic การ validation ระดับล่าง เช่น “product ต้องขึ้นต้นด้วย W หรือ G ” ไม่ได้ถูก implement อย่างชัดเจนใน validation function ของเรา แต่ถูกฝังไว้ใน constructor ของ constrained simple type เช่น OrderId และ ProductCode การใช้ type ช่วยเพิ่มความมั่นใจได้ว่า code ถูกต้อง—ความจริงที่ว่าเราสามารถสร้าง ValidatedOrder จาก UnvalidatedOrder ได้สำเร็จ หมายความว่าเราเชื่อใจได้เลยว่ามันผ่านการ validate แล้ว!