เราได้สร้าง type ไปมากมายในบทนี้ ดังนั้นลองถอยกลับมามองดูว่าพวกมันประกอบกันเป็นภาพรวมอย่างไร ในฐานะ domain model ที่สมบูรณ์

ก่อนอื่น เราจะใส่ type ทั้งหมดนี้ไว้ใน namespace ที่ชื่อ OrderTaking.Domain ซึ่งใช้แยก type เหล่านี้ออกจาก namespace อื่นๆ พูดอีกอย่างก็คือ เรากำลังใช้ namespace ใน F# เพื่อบ่งบอกถึง DDD bounded context อย่างน้อยก็ในตอนนี้

​  ​ namespace ​ OrderTaking.Domain
​  
​  ​ // types follow ​

ต่อไปมาเพิ่ม simple type กันก่อน

​  ​ // Product code related ​
​  ​ type ​ WidgetCode = WidgetCode ​ of ​ ​ string ​
​  ​ // constraint: starting with "W" then 4 digits ​
​  ​ type ​ GizmoCode = GizmoCode ​ of ​ ​ string ​
​  ​ // constraint: starting with "G" then 3 digits ​
​  ​ type ​ ProductCode =
​  | Widget ​ of ​ WidgetCode
​  | Gizmo ​ of ​ GizmoCode
​  
​  ​ // Order Quantity related ​
​  ​ type ​ UnitQuantity = UnitQuantity ​ of ​ ​ int ​
​  ​ type ​ KilogramQuantity = KilogramQuantity ​ of ​ decimal
​  ​ type ​ OrderQuantity =
​  | Unit ​ of ​ UnitQuantity
​  | Kilos ​ of ​ KilogramQuantity

ทั้งหมดนี้เป็น value objects และไม่จำเป็นต้องมี identifier

ในทางกลับกัน order มี identity ที่คงอยู่แม้จะเปลี่ยนแปลงไป มันคือ Entity ดังนั้นเราต้องโมเดลมันด้วย ID เรายังไม่รู้ว่า ID นั้นจะเป็น string หรือ int หรือ Guid แต่เรารู้ว่าเราต้องการมัน ดังนั้นตอนนี้ลองใช้ Undefined ไปก่อน เราจะจัดการกับ identifier อื่นๆ ในลักษณะเดียวกัน

​  ​ type ​ OrderId = Undefined
​  ​ type ​ OrderLineId = Undefined
​  ​ type ​ CustomerId = Undefined

ตอนนี้เราสามารถร่าง order และส่วนประกอบของมันได้แล้ว:

​  ​ type ​ CustomerInfo = Undefined
​  ​ type ​ ShippingAddress = Undefined
​  ​ type ​ BillingAddress = Undefined
​  ​ type ​ Price = Undefined
​  ​ type ​ BillingAmount = Undefined
​  
​  ​ type ​ Order = {
​  Id : OrderId ​ // id for entity ​
​  CustomerId : CustomerId ​ // customer reference ​
​  ShippingAddress : ShippingAddress
​  BillingAddress : BillingAddress
​  OrderLines : OrderLine ​ list ​
​  AmountToBill : BillingAmount
​  }
​  
​  ​ and ​ OrderLine = {
​  Id : OrderLineId ​ // id for entity ​
​  OrderId : OrderId
​  ProductCode : ProductCode
​  OrderQuantity : OrderQuantity
​  Price : Price
​  }

ในโค้ดข้างต้น เราใช้คีย์เวิร์ด and เพื่อให้สามารถอ้างอิงล่วงหน้า (forward reference) ไปยัง type ที่ยังไม่ได้ประกาศได้ ดูคำอธิบายเพิ่มเติมได้ที่ ​ Organizing Types in Files and Projects ​ ​

ทีนี้มาปิดท้ายด้วยตัว workflow เอง input ของ workflow คือ UnvalidatedOrder จะถูกสร้างขึ้นมาจากแบบฟอร์ม order "ตามที่มันเป็น" ดังนั้นมันจะมีแค่ primitive types เช่น int และ string เท่านั้น

​  ​ type ​ UnvalidatedOrder = {
​  OrderId : ​ string ​
​  CustomerInfo : ...
​  ShippingAddress : ...
​  ...
​  }

เราต้องการ type สองแบบสำหรับ output ของ workflow แบบแรกคือ type ของ event สำหรับกรณีที่ workflow สำเร็จ:

​  ​ type ​ PlaceOrderEvents = {
​  AcknowledgmentSent : ...
​  OrderPlaced : ...
​  BillableOrderPlaced : ...
​  }

แบบที่สองคือ type ของ error สำหรับกรณีที่ workflow ล้มเหลว:

​  ​ type ​ PlaceOrderError =
​  | ValidationError ​ of ​ ValidationError ​ list ​
​  | ... ​ // other errors ​
​  
​  ​ and ​ ValidationError = {
​  FieldName : ​ string ​
​  ErrorDescription : ​ string ​
​  }

สุดท้ายนี้ เราสามารถนิยาม function ระดับบนสุดที่แทนความหมายของ workflow การรับ order ได้:

​  ​ /// The "Place Order" process ​
​  ​ type ​ PlaceOrder =
​  UnvalidatedOrder -> Result<PlaceOrderEvents,PlaceOrderError>

เห็นได้ชัดว่ายังมีรายละเอียดอีกมากที่ต้องเติมให้สมบูรณ์ แต่ตอนนี้กระบวนการในการทำเช่นนั้นก็น่าจะชัดเจนแล้ว

อย่างไรก็ตาม โมเดลของ workflow การรับ order ของเรายังไม่สมบูรณ์ ตัวอย่างเช่น เราจะโมเดลสถานะต่างๆ ของ order อย่างไร ทั้ง validated, priced และอื่นๆ?

The Challenge Revisited: Can Types Replace Documentation? (ย้อนกลับมาที่โจทย์: Type แทนที่เอกสารได้หรือไม่?)

ตอนต้นบทนี้ เราได้ตั้งโจทย์ท้าทายให้ตัวเองไว้ว่า เราจะสามารถเก็บ requirement ของ domain ไว้ใน type system ได้หรือไม่ และในลักษณะที่ domain expert และคนที่ไม่ใช่นักพัฒนาสามารถตรวจสอบได้ด้วย?

เอาล่ะ ถ้าเรามองดู domain model ที่ list ไว้ด้านบน เราน่าจะพอใจได้ เรามี domain model ที่สมบูรณ์ ซึ่งบันทึกไว้เป็น F# type แทนที่จะเป็นข้อความ แต่ type ที่เราออกแบบไว้นั้นมีหน้าตาแทบจะเหมือนกับเอกสาร domain ที่เราพัฒนาขึ้นก่อนหน้านี้โดยใช้สัญลักษณ์ AND และ OR เลยทีเดียว

ลองจินตนาการว่าคุณเป็นคนที่ไม่ใช่นักพัฒนา คุณจะต้องเรียนรู้อะไรบ้างเพื่อจะเข้าใจ code นี้ในฐานะเอกสาร? คุณต้องเข้าใจไวยากรณ์ของ simple type (single-case union), type แบบ AND (record ที่ใช้วงเล็บปีกกา), type แบบ OR (choice ที่ใช้เส้นแนวตั้งคั่น) และ "process" (input, output และลูกศร) แต่ก็แค่นั้นเอง ไม่มีอะไรมากไปกว่านี้ มันอ่านง่ายกว่าภาษาโปรแกรมทั่วไปอย่าง C# หรือ Java อย่างแน่นอน