มาต่อกันที่สถานการณ์ที่แตกต่างออกไป ทีมขายต้องการทำโปรโมชันบางอย่าง และต้องการเสนอ promotion code ที่สามารถกรอกตอนสั่งซื้อ เพื่อรับราคาที่ลดลง
หลังจากคุยกับทีมขายแล้ว เรามี requirement ใหม่เหล่านี้
- เมื่อสั่งซื้อ ลูกค้าสามารถกรอก promotion code ได้ (ไม่บังคับ)
- ถ้ามี promotion code นี้ สินค้าบางรายการจะได้ราคาที่แตกต่างกัน (ต่ำกว่า)
- คำสั่งซื้อควรแสดงให้เห็นว่ามีการใช้ส่วนลดโปรโมชัน
การเปลี่ยนแปลงบางอย่างในนี้ทำได้ง่าย แต่ requirement สุดท้ายที่ดูเหมือนง่ายมากนี้ จะสร้างผลกระทบที่ทรงพลังอย่างน่าประหลาดใจไปทั่วทั้ง domain
Adding a Promotion Code to the Domain Model (การเพิ่มโค้ดโปรโมชันเข้าไปใน Domain Model)
มาเริ่มกันที่ฟิลด์ promotion code ตัวใหม่ เหมือนเดิม เราจะอัปเดต domain model แล้วดูว่ามันจะนำเราไปที่ไหน
เราจะเริ่มด้วยการนิยาม type สำหรับ promotion code แล้วเพิ่มมันเป็นฟิลด์ที่ไม่บังคับของคำสั่งซื้อ
| | type PromotionCode = PromotionCode of string |
| | |
| | type ValidatedOrder = { |
| | ... |
| | PromotionCode : PromotionCode option |
| | } |
ไม่มี validation พิเศษสำหรับ PromotionCode แต่การใช้ type แทนที่จะเป็น string ธรรมดาก็เป็นความคิดที่ดี เพื่อไม่ให้มันปนกับ string อื่นๆ ใน domain
เช่นเดียวกับฟิลด์ VipStatus ก่อนหน้านี้ การเพิ่มฟิลด์ใหม่จะกระตุ้นให้เกิด compiler errors ชุดหนึ่ง ในกรณีนี้ เราจะต้องเพิ่มฟิลด์ที่สอดคล้องกันใน UnvalidatedOrder และใน DTO ด้วยเช่นกัน สังเกตว่าแม้ว่าฟิลด์ใน ValidatedOrder จะถูกทำเครื่องหมายว่าไม่บังคับอย่างชัดเจน แต่เราสามารถใช้ string ที่ไม่ใช่ option ใน DTO ได้ โดยสมมติว่า null จะบ่งบอกถึงค่าที่หายไป
| | type OrderDto = { |
| | ... |
| | PromotionCode : string |
| | } |
| | |
| | type UnvalidatedOrder = { |
| | ... |
| | PromotionCode : string |
| | } |
Changing the Pricing Logic (การเปลี่ยนแปลง Pricing Logic)
ถ้ามี promotion code เราต้องทำการคำนวณราคาแบบหนึ่ง และถ้าไม่มี เราต้องทำอีกแบบหนึ่ง เราจะโมเดลสิ่งนี้ใน domain ได้อย่างไร?
ที่จริงแล้ว เราได้โมเดลการคำนวณราคาโดยใช้ function type ไปแล้ว
| | type GetProductPrice = ProductCode -> Price |
แต่ตอนนี้เราต้องมี function GetProductPrice ที่แตกต่างกันตาม promotion code นี่คือ logic
-
ถ้ามี promotion code ให้ใช้ function GetProductPrice ที่คืนราคาที่เกี่ยวข้องกับ promotion code นั้น
-
ถ้าไม่มี promotion code ให้ใช้ function GetProductPrice ดั้งเดิม
สิ่งที่เราต้องมีคือ function "factory" ที่เมื่อได้รับ promotion code แบบ optional จะคืน function GetProductPrice ที่เหมาะสม แบบนี้
| | type GetPricingFunction = PromotionCode option -> GetProductPrice |
แต่การส่ง option แบบนี้ดูจะไม่ค่อยชัดเจนนัก ดังนั้นบางทีเราควรสร้าง type ใหม่ที่อธิบายตัวเองได้ดีกว่าไหม?
| | type PricingMethod = |
| | | Standard |
| | | Promotion of PromotionCode |
ในเชิงตรรกะ นี่เทียบเท่ากับ option แต่มันชัดเจนกว่าเล็กน้อยเมื่อใช้ใน domain model type ValidatedOrder ตอนนี้จะมีหน้าตาแบบนี้
| | type ValidatedOrder = { |
| | ... //as before |
| | PricingMethod : PricingMethod |
| | } |
และ GetPricingFunction จะมีหน้าตาแบบนี้
| | type GetPricingFunction = PricingMethod -> GetProductPrice |
ยังมีอีกอย่างที่ต้องเปลี่ยน ในการออกแบบเดิม เราฉีด function GetProductPrice เข้าไปใน pricing stage ของ workflow ตอนนี้เราต้องฉีด function "factory" GetPricingFunction เข้าไปใน pricing step แทน
| | type PriceOrder = |
| | GetPricingFunction // new dependency |
| | -> ValidatedOrder // input |
| | -> PricedOrder // output |
เมื่อคุณเปลี่ยนแปลง domain model เหล่านี้เสร็จแล้ว คุณจะได้ compiler errors จำนวนมากในการ implement อีกครั้ง อย่างไรก็ตาม compiler errors เหล่านี้คือเพื่อนของคุณ! มันจะนำทางคุณว่าต้องทำอะไรบ้างเพื่อแก้ไข implementation มันเป็นกระบวนการที่น่าเบื่อแต่ตรงไปตรงมา เมื่อคุณทำเสร็จแล้ว และ implementation compile ผ่านอีกครั้ง คุณก็มั่นใจได้อย่างมากว่าทุกอย่างทำงานได้โดยไม่มี error
Implementing the GetPricingFunction (การ Implement GetPricingFunction)
มาดูกันคร่าวๆ ว่า GetPricingFunction จะ implement ได้อย่างไร เราจะสมมติว่า promotion code แต่ละตัวถูกผูกกับ dictionary ของคู่ ( ProductCode , Price ) ในกรณีนั้น การ implement อาจมีหน้าตาประมาณนี้
| | type GetStandardPriceTable = |
| | // no input -> return standard prices |
| | unit -> IDictionary<ProductCode,Price> |
| | |
| | type GetPromotionPriceTable = |
| | // promo input -> return prices for promo |
| | PromotionCode -> IDictionary<ProductCode,Price> |
| | |
| | let getPricingFunction |
| | (standardPrices:GetStandardPriceTable) |
| | (promoPrices:GetPromotionPriceTable) |
| | : GetPricingFunction = |
| | |
| | |
| | // the original pricing function |
| | let getStandardPrice : GetProductPrice = |
| | // cache the standard prices |
| | let standardPrices = standardPrices() |
| | // return the lookup function |
| | fun productCode -> standardPrices.[productCode] |
| | |
| | // the promotional pricing function |
| | let getPromotionPrice promotionCode : GetProductPrice = |
| | // cache the promotional prices |
| | let promotionPrices = promoPrices promotionCode |
| | // return the lookup function |
| | fun productCode -> |
| | match promotionPrices.TryGetValue productCode with |
| | // found in promotional prices |
| | | true,price -> price |
| | // not found in promotional prices |
| | // so use standard price |
| | | false, _ -> getStandardPrice productCode |
| | |
| | // return a function that conforms to GetPricingFunction |
| | fun pricingMethod -> |
| | match pricingMethod with |
| | | Standard -> |
| | getStandardPrice |
| | | Promotion promotionCode -> |
| | getPromotionPrice promotionCode |
เราจะไม่ลงรายละเอียดอธิบาย code นี้ เพราะมันค่อนข้างอธิบายตัวเองอยู่แล้ว แต่คุณจะเห็นว่าเรากำลังใช้เทคนิค functional programming ที่เราชื่นชอบหลายอย่าง คือ type เพื่อรับประกันว่า code ถูกต้อง ( GetProductPrice ) และเพื่อทำให้ domain logic ชัดเจน (ตัวเลือกใน PricingMethod ), function ในฐานะพารามิเตอร์ ( promoPrices ที่มี type GetPromotionPriceTable ), และ function ในฐานะ output (ค่าที่คืนกลับของ type GetPricingFunction )
Documenting the Discount in an Order Line (การบันทึกส่วนลดไว้ใน Order Line)
หนึ่งใน requirement คือ "คำสั่งซื้อควรแสดงให้เห็นว่ามีการใช้ส่วนลดโปรโมชัน" เราควรทำแบบนั้นอย่างไร?
เพื่อตอบคำถามนั้น เราต้องรู้ว่าระบบปลายทาง (downstream) จำเป็นต้องรู้เกี่ยวกับโปรโมชันหรือไม่ ถ้าไม่จำเป็น option ที่ง่ายที่สุดคือแค่เพิ่ม "comment line" เข้าไปใน list ของ order line เราไม่จำเป็นต้องมีรายละเอียดพิเศษใดๆ ใน comment แค่ข้อความอธิบายส่วนลดก็พอ
นั่นหมายความว่าเราต้องเปลี่ยนนิยามของ "order line" จนถึงตอนนี้ เราสมมติมาตลอดว่า line ของคำสั่งซื้อจะอ้างอิงถึงสินค้าเสมอ แต่ตอนนี้เราต้องบอกว่ามี order line ชนิดใหม่ที่ ไม่ อ้างอิงถึงสินค้า นั่นคือการเปลี่ยนแปลง domain model ของเรา มาเปลี่ยนนิยามของ PricedOrderLine ให้สะท้อนสิ่งนี้ โดยทำให้มันเป็น choice type กัน
| | type CommentLine = CommentLine of string |
| | |
| | type PricedOrderLine = |
| | | Product of PricedOrderProductLine |
| | | Comment of CommentLine |
ไม่จำเป็นต้องมี validation พิเศษใดๆ สำหรับ type CommentLine ยกเว้นบางทีอาจต้องตรวจสอบว่าจำนวนตัวอักษรไม่มากเกินไป
ถ้าเรา ต้องการ ติดตามรายละเอียดมากกว่าแค่ comment เราอาจนิยามเคส DiscountApplied แทน ที่มีข้อมูลเช่นจำนวนเงินส่วนลด และอื่นๆ ข้อดีของการใช้ Comment คือ shipping context และ billing context ไม่จำเป็นต้องรู้อะไรเกี่ยวกับโปรโมชันเลย ดังนั้นถ้า logic ของโปรโมชันเปลี่ยนไป พวกมันก็จะไม่ได้รับผลกระทบ
ทีนี้ เพราะเราได้เปลี่ยน PricedOrderLine ให้เป็น choice type เราจึงต้องมี type ใหม่ชื่อ PricedOrderProductLine ที่เก็บรายละเอียดของ line ที่เกี่ยวข้องกับสินค้า เช่น ราคา จำนวน และอื่นๆ
สุดท้าย เห็นได้ชัดว่า ValidatedOrderLine กับ PricedOrderLine ตอนนี้แยกออกจากกันในการออกแบบแล้ว นี่แสดงให้เห็นว่าการแยก type ออกจากกันระหว่างการทำ domain modeling เป็นความคิดที่ดี คุณไม่มีทางรู้ว่าเมื่อไหร่ที่การเปลี่ยนแปลงแบบนี้จะจำเป็น และถ้า type เดียวกันถูกใช้ทั้งสองแบบ เราก็คงไม่สามารถรักษาความสะอาดของโมเดลไว้ได้
เพื่อเพิ่ม comment line function priceOrder ต้องถูกเปลี่ยน
- ก่อนอื่น ดึง function pricing มาจาก "factory" GetPricingFunction
- ต่อไป สำหรับแต่ละ line ให้กำหนดราคาโดยใช้ function pricing นั้น
- สุดท้าย ถ้ามีการใช้ promotion code ให้เพิ่ม comment line พิเศษเข้าไปใน list ของ line
| | let toPricedOrderLine orderLine = ... |
| | |
| | let priceOrder : PriceOrder = |
| | fun getPricingFunction validatedOrder -> |
| | // get the pricing function from the getPricingFunction "factory" |
| | let getProductPrice = getPricingFunction validatedOrder.PricingMethod |
| | |
| | // set the price for each line |
| | let productOrderLines = |
| | validatedOrder.OrderLines |
| | |> List.map (toPricedOrderLine getProductPrice) |
| | |
| | // add the special comment line if needed |
| | let orderLines = |
| | match validatedOrder.PricingMethod with |
| | | Standard -> |
| | // unchanged |
| | productOrderLines |
| | | Promotion promotion -> |
| | let promoCode = promotion|> PromotionCode.value |
| | let commentLine = |
| | sprintf "Applied promotion %s" promoCode |
| | |> CommentLine.create |
| | |> Comment // lift to PricedOrderLine |
| | List.append productOrderLines [commentLine] |
| | |
| | // return the new order |
| | { |
| | ... |
| | OrderLines = orderLines |
| | } |
More Complicated Pricing Schemes (Pricing Scheme ที่ซับซ้อนขึ้น)
ในหลายกรณี pricing scheme สามารถซับซ้อนขึ้นไปอีกได้ ด้วยโปรโมชันการขายหลายแบบ บัตรกำนัล โปรแกรมสะสมแต้ม และอื่นๆ ถ้าเกิดเหตุการณ์แบบนี้ขึ้น มันอาจเป็นสัญญาณว่า pricing จำเป็นต้องกลายเป็น bounded context แยกต่างหาก จำการพูดคุยเรื่อง getting the bounded contexts right ได้ไหม? นี่คือเบาะแส
-
คำศัพท์เฉพาะที่แตกต่างออกไป (ด้วยศัพท์เฉพาะวงการอย่าง "BOGOF" [43] )
-
ทีมพิเศษที่ดูแลราคา
-
ข้อมูลที่เฉพาะเจาะจงกับ context นี้เท่านั้น (เช่น ประวัติการซื้อและการใช้บัตรกำนัล)
-
ความสามารถในการทำงานได้อย่างอิสระ (autonomous)
ถ้า pricing เป็นส่วนสำคัญของธุรกิจ ก็สำคัญไม่แพ้กันที่มันจะสามารถวิวัฒนาการได้อย่างเป็นอิสระและแยกตัวออกจาก order-taking, shipping และ billing domain นี่คือไดอะแกรมที่แสดง pricing เป็น bounded context ที่แยกออกมาต่างหาก ซึ่งสัมพันธ์ใกล้ชิดกับ order-taking แต่ตอนนี้แยกกันในเชิงตรรกะแล้ว
Evolving the Contract Between Bounded Contexts (การพัฒนา Contract ระหว่าง Bounded Context)
เราได้เพิ่ม line ชนิดใหม่เข้ามา คือ CommentLine ซึ่งระบบ shipping จำเป็นต้องรู้เพื่อพิมพ์คำสั่งซื้อได้อย่างถูกต้อง นั่นหมายความว่า event OrderPlaced ที่ส่งไปยังปลายทางจะต้องถูกเปลี่ยนแปลงด้วยเช่นกัน
และตอนนี้เราก็เพิ่งทำลาย contract ระหว่าง order-taking context กับ shipping context ไปแล้ว
สิ่งนี้ยั่งยืนไหม? นั่นคือ ทุกครั้งที่เราเพิ่มแนวคิดใหม่เข้าไปใน order-taking domain เราต้องเปลี่ยน event และ DTO แล้วทำลาย contract จริงๆ หรือ? แน่นอนว่าไม่ควรเป็นแบบนั้น แต่ในสภาพปัจจุบัน เราได้สร้าง coupling ระหว่าง bounded contexts ขึ้นมาแล้ว ซึ่งไม่ใช่สิ่งที่เราต้องการอย่างแน่นอน
ดังที่ได้พูดถึงไปก่อนหน้านี้ใน Contracts Between Bounded Contexts ทางแก้ที่ดีสำหรับปัญหานี้คือใช้ contract แบบ "consumer-driven" ในแนวทางนี้ ผู้บริโภค (downstream) เป็นคนตัดสินใจว่าต้องการอะไรจากผู้ผลิต (upstream) และผู้ผลิตต้องให้ข้อมูลนั้นเท่านั้น ไม่มากไปกว่านั้น
ในสถานการณ์นี้ มาคิดกันว่า shipping context จริงๆ แล้ว ต้องการอะไร มันไม่ต้องการราคา ไม่ต้องการค่าจัดส่ง ไม่ต้องการข้อมูลส่วนลด สิ่งเดียวที่มันต้องการคือ list ของสินค้า จำนวนของแต่ละรายการ และที่อยู่จัดส่ง มาออกแบบ type เพื่อแทนสิ่งนี้กัน
| | type ShippableOrderLine = { |
| | ProductCode : ProductCode |
| | Quantity : float |
| | } |
| | |
| | type ShippableOrderPlaced = { |
| | OrderId : OrderId |
| | ShippingAddress : Address |
| | ShipmentLines : ShippableOrderLine list |
| | } |
นี่ง่ายกว่า event type OrderPlaced ดั้งเดิมมาก และเพราะมันมีข้อมูลน้อยกว่า มันจึงมีโอกาสน้อยกว่าที่จะต้องเปลี่ยนแปลงเมื่อ order-taking domain เปลี่ยนแปลง
ด้วย event type ใหม่นี้ในมือ เราควรออกแบบ output PlaceOrderEvent ของ order-taking workflow ใหม่ ตอนนี้เรามีสิ่งต่อไปนี้
- AcknowledgmentSent สำหรับบันทึกและส่งให้ customer service context
- ShippableOrderPlaced สำหรับส่งให้ shipping context
- BillableOrderPlaced สำหรับส่งให้ billing context
| | type PlaceOrderEvent = |
| | | ShippableOrderPlaced of ShippableOrderPlaced |
| | | BillableOrderPlaced of BillableOrderPlaced |
| | | AcknowledgmentSent of OrderAcknowledgmentSent |
Printing the Order (การพิมพ์คำสั่งซื้อ)
แต่แล้วการพิมพ์คำสั่งซื้อล่ะ? เมื่อคำสั่งซื้อถูกจัดเตรียมและพร้อมสำหรับจัดส่ง สำเนาของคำสั่งซื้อต้นฉบับจะถูกพิมพ์ออกมาและใส่ไว้ในพัสดุ แผนกจัดส่งจะพิมพ์คำสั่งซื้อได้อย่างไร ในเมื่อเราจงใจลดข้อมูลที่มันเข้าถึงได้ลง?
กุญแจสำคัญของเรื่องนี้คือการตระหนักว่าแผนกจัดส่งแค่ต้องการสิ่งที่มันพิมพ์ได้ แต่มันไม่ได้สนใจเนื้อหาจริงๆ กล่าวอีกนัยหนึ่งคือ order-placing context สามารถให้ PDF หรือเอกสาร HTML แก่แผนกจัดส่ง แล้วให้มันพิมพ์เอกสารนั้น
เอกสารนี้อาจถูกให้มาเป็น binary blob ใน type ShippableOrderPlaced ข้างต้น หรือเราอาจดัมพ์ PDF ลงใน shared storage แล้วให้ shipping context เข้าถึงมันผ่าน OrderId