เราได้ผ่านการออกแบบรอบแรกมาแล้ว ทีนี้มาทบทวนสิ่งที่เรามีจนถึงตอนนี้กัน ก่อนอื่นเราจะเขียน type ของ public API ลงไป โดยทั่วไปเราจะเก็บมันไว้ในไฟล์เดียวกันทั้งหมด เช่น DomainApi.fs หรือชื่อคล้ายๆ กัน
นี่คือ inputs ทั้งหมด
| | // ---------------------- |
| | // Input data |
| | // ---------------------- |
| | |
| | type UnvalidatedOrder = { |
| | OrderId : string |
| | CustomerInfo : UnvalidatedCustomer |
| | ShippingAddress : UnvalidatedAddress |
| | } |
| | and UnvalidatedCustomer = { |
| | Name : string |
| | Email : string |
| | } |
| | and UnvalidatedAddress = ... |
| | |
| | // ---------------------- |
| | // Input Command |
| | // ---------------------- |
| | |
| | type Command< ' data> = { |
| | Data : ' data |
| | Timestamp: DateTime |
| | UserId: string |
| | // etc |
| | } |
| | |
| | type PlaceOrderCommand = Command<UnvalidatedOrder> |
และนี่คือ outputs และนิยามของ workflow เอง
| | // ---------------------- |
| | // Public API |
| | // ---------------------- |
| | |
| | /// Success output of PlaceOrder workflow |
| | type OrderPlaced = ... |
| | type BillableOrderPlaced = ... |
| | type OrderAcknowledgmentSent = ... |
| | type PlaceOrderEvent = |
| | | OrderPlaced of OrderPlaced |
| | | BillableOrderPlaced of BillableOrderPlaced |
| | | AcknowledgmentSent of OrderAcknowledgmentSent |
| | |
| | /// Failure output of PlaceOrder workflow |
| | type PlaceOrderError = ... |
| | |
| | type PlaceOrderWorkflow = |
| | PlaceOrderCommand // input command |
| | -> AsyncResult<PlaceOrderEvent list ,PlaceOrderError> // output events |
The Internal Steps (ขั้นตอนภายใน)
เราจะเก็บ type ที่ใช้ในขั้นตอนภายในไว้ในไฟล์ implementation แยกต่างหาก (เช่น PlaceOrderWorkflow.fs ) ต่อมาในภายหลัง ที่ด้านล่างของไฟล์เดียวกันนี้ เราจะเพิ่ม implementation เข้าไป
เราจะเริ่มด้วย state ภายในที่แทน life cycle ของ order
| | // bring in the types from the domain API module |
| | open DomainApi |
| | |
| | // ---------------------- |
| | // Order life cycle |
| | // ---------------------- |
| | |
| | // validated state |
| | type ValidatedOrderLine = ... |
| | type ValidatedOrder = { |
| | OrderId : OrderId |
| | CustomerInfo : CustomerInfo |
| | ShippingAddress : Address |
| | BillingAddress : Address |
| | OrderLines : ValidatedOrderLine list |
| | } |
| | and OrderId = Undefined |
| | and CustomerInfo = ... |
| | and Address = ... |
| | |
| | // priced state |
| | type PricedOrderLine = ... |
| | type PricedOrder = ... |
| | |
| | // all states combined |
| | type Order = |
| | | Unvalidated of UnvalidatedOrder |
| | | Validated of ValidatedOrder |
| | | Priced of PricedOrder |
| | // etc |
จากนั้นเราสามารถเพิ่มนิยามสำหรับแต่ละขั้นตอนภายในได้
| | // ---------------------- |
| | // Definitions of Internal Steps |
| | // ---------------------- |
| | |
| | // ----- Validate order ----- |
| | |
| | // services used by ValidateOrder |
| | type CheckProductCodeExists = |
| | ProductCode -> bool |
| | |
| | type AddressValidationError = ... |
| | type CheckedAddress = ... |
| | type CheckAddressExists = |
| | UnvalidatedAddress |
| | -> AsyncResult<CheckedAddress,AddressValidationError> |
| | |
| | type ValidateOrder = |
| | CheckProductCodeExists // dependency |
| | -> CheckAddressExists // dependency |
| | -> UnvalidatedOrder // input |
| | -> AsyncResult<ValidatedOrder,ValidationError list > // output |
| | and ValidationError = ... |
| | |
| | // ----- Price order ----- |
| | |
| | // services used by PriceOrder |
| | type GetProductPrice = |
| | ProductCode -> Price |
| | |
| | type PricingError = ... |
| | |
| | type PriceOrder = |
| | GetProductPrice // dependency |
| | -> ValidatedOrder // input |
| | -> Result<PricedOrder,PricingError> // output |
| | |
| | // etc |
ตอนนี้เรามี type ทั้งหมดอยู่ในที่เดียวกันแล้ว พร้อมที่จะใช้เป็นแนวทางสำหรับ implementation ต่อไป