กระบวนการ serialization ก็เป็นเพียงอีกหนึ่ง component ที่สามารถเพิ่มเข้าไปใน workflow pipeline ได้ นั่นคือขั้นตอน deserialization จะถูกเพิ่มไว้ที่จุดเริ่มต้นของ workflow ส่วนขั้นตอน serialization จะถูกเพิ่มไว้ที่จุดสิ้นสุดของ workflow
ตัวอย่างเช่น สมมติว่าเรามี workflow ที่มีหน้าตาแบบนี้ (เราจะข้ามเรื่อง error handling และ Results ไปก่อน):
| | type MyInputType = ... |
| | type MyOutputType = ... |
| | |
| | type Workflow = MyInputType -> MyOutputType |
จากนั้น function signature สำหรับขั้นตอน deserialization อาจมีหน้าตาแบบนี้:
| | type JsonString = string |
| | type MyInputDto = ... |
| | |
| | type DeserializeInputDto = JsonString -> MyInputDto |
| | type InputDtoToDomain = MyInputDto -> MyInputType |
และขั้นตอน serialization อาจมีหน้าตาแบบนี้:
| | type MyOutputDto = ... |
| | |
| | type OutputDtoFromDomain = MyOutputType -> MyOutputDto |
| | type SerializeOutputDto = MyOutputDto -> JsonString |
เห็นได้ชัดว่า function ทั้งหมดนี้สามารถเชื่อมต่อกันเป็น pipeline ได้ แบบนี้:
| | let workflowWithSerialization jsonString = |
| | jsonString |
| | |> deserializeInputDto // JSON to DTO |
| | |> inputDtoToDomain // DTO to domain object |
| | |> workflow // the core workflow in the domain |
| | |> outputDtoFromDomain // Domain object to DTO |
| | |> serializeOutputDto // DTO to JSON |
| | // final output is another JsonString |
และจากนั้น function workflowWithSerialization นี้ก็จะเป็นตัวที่ถูก expose ออกไปให้ infrastructure ใช้ ทั้ง input และ output เป็นแค่ JsonStrings หรืออะไรทำนองนั้น เพื่อให้ infrastructure ถูกแยกออกจาก domain
แน่นอนว่าในทางปฏิบัติมันไม่ได้ง่ายขนาดนั้น! เราต้องจัดการเรื่อง error, async และอื่นๆ ด้วย แต่ตัวอย่างนี้ก็แสดงให้เห็นแนวคิดพื้นฐานแล้ว
DTOs as a Contract Between Bounded Contexts (DTO ในฐานะสัญญาระหว่าง Bounded Context)
command ที่เรารับเข้ามาถูก trigger โดย output ของ bounded contexts อื่นๆ และ event ที่ workflow ของเรา emit ออกไปก็จะกลายเป็น input ให้กับ bounded contexts อื่นๆ event และ command เหล่านี้ก่อตัวเป็นสัญญา (contract) แบบหนึ่งที่ bounded context ของเราต้องรองรับ มันเป็นสัญญาที่หลวมๆ แน่นอน เพราะเราต้องการหลีกเลี่ยง tight coupling ระหว่าง bounded contexts อย่างไรก็ตาม รูปแบบ serialization ของ event และ command เหล่านี้ ซึ่งก็คือ DTO ควรถูกเปลี่ยนแปลงอย่างระมัดระวังเท่านั้น หรือไม่เปลี่ยนแปลงเลย นั่นหมายความว่าคุณควรควบคุม format ของ serialization ได้อย่างสมบูรณ์เสมอ และไม่ควรปล่อยให้ library ทำสิ่งต่างๆ โดยอัตโนมัติแบบมายากล!