เรามี low-level helper function จำนวนหนึ่ง เช่น toValidProductCode ที่รับ function parameter ซึ่งเป็นตัวแทนของ service ฟังก์ชันเหล่านี้อยู่ลึกในดีไซน์พอสมควร แล้วเราจะส่ง dependency จาก top level ลงไปยัง function ที่ต้องการมันได้ยังไง

ถ้าเราทำ object-oriented programming เราจะใช้ dependency injection และอาจใช้ IoC container ด้วย แต่ใน functional programming เราไม่อยากทำแบบนั้น เพราะ dependency จะกลายเป็นสิ่งที่ซ่อนอยู่ (implicit) แทนที่จะเป็นแบบนั้น เราต้องการส่ง dependency ไปมาแบบชัดเจนเสมอผ่าน parameter ซึ่งช่วยให้ dependency มองเห็นได้ชัดเจน

มีเทคนิคหลายแบบสำหรับการทำสิ่งนี้ใน functional programming เช่น “Reader Monad” และ “Free Monad” แต่เนื่องจากนี่เป็นหนังสือเบื้องต้น เราจะยึดแนวทางที่ง่ายที่สุด คือส่ง dependency ทั้งหมดเข้าไปใน function ระดับบนสุด แล้ว function นั้นจะส่งต่อให้ function ภายใน ซึ่งจะส่งต่อไปยัง function ภายในของ มัน อีกที และเป็นแบบนี้ไปเรื่อยๆ

สมมติว่าเรา implement helper function ตามที่เรานิยามไว้ก่อนหน้านี้:

​  ​ // low-level helper functions ​
​  ​ let ​ toAddress checkAddressExists unvalidatedAddress =
​  ...
​  
​  ​ let ​ toProductCode checkProductCodeExists productCode =
​  ...

ทั้งสอง function มี parameter ที่ชัดเจนสำหรับ dependency ของมัน

ตอนนี้ในฐานะที่เป็นส่วนหนึ่งของการสร้าง order line เราต้องสร้าง product code ด้วย นั่นหมายความว่า toValidatedOrderLine ต้องใช้ toProductCode ซึ่งหมายความว่า toValidatedOrderLine ก็ต้องมี parameter checkProductCodeExists ด้วยเช่นกัน:

​  ​ // helper function ​
​  ​ let ​ toValidatedOrderLine checkProductExists unvalidatedOrderLine =
​  ​ // ^ needed for toProductCode, below ​
​  
​  ​ // create the components of the line ​
​  ​ let ​ orderLineId = ...
​  ​ let ​ productCode =
​  unvalidatedOrderLine.ProductCode
​  |> toProductCode checkProductExists ​ //use service ​
​  
​  ...

และเมื่อขยับขึ้นไปอีกระดับ function validateOrder ต้องใช้ทั้ง toAddress และ toValidatedOrderLine ดังนั้นมันจึงต้องมี service ทั้งสอง ส่งเข้ามาเป็น parameter เพิ่ม:

​  ​ let ​ validateOrder : ValidateOrder =
​  ​ fun ​ checkProductExists ​ // dependency for toValidatedOrderLine ​
​  checkAddressExists ​ // dependency for toAddress ​
​  unvalidatedOrder ->
​  
​  ​ // build the validated address using the dependency ​
​  ​ let ​ shippingAddress =
​  unvalidatedOrder.ShippingAddress
​  |> toAddress checkAddressExists
​  
​  ...
​  
​  ​ // build the validated order lines using the dependency ​
​  ​ let ​ lines =
​  unvalidatedOrder.Lines
​  |> List.map (toValidatedOrderLine checkProductExists)
​  
​  ...

และเป็นแบบนี้ไปเรื่อยๆ ตามสาย จนกว่าจะไปถึง function ระดับบนสุดที่ตั้งค่า service และ dependency อื่นๆ ทั้งหมด ในการออกแบบแบบ object-oriented function ระดับบนสุดนี้มักถูกเรียกว่า composition root ดังนั้นเรามาใช้คำนี้กันตรงนี้เช่นกัน

แล้ว workflow function placeOrder ควรทำหน้าที่เป็น composition root ไหม ไม่ควร เพราะการตั้งค่า service มักเกี่ยวข้องกับการเข้าถึง configuration และอื่นๆ ทางที่ดีกว่าคือให้ workflow placeOrder เองได้รับ service ที่มันต้องการเป็น parameter แบบนี้:

​  ​ let ​ placeOrder
​  checkProductExists ​ // dependency ​
​  checkAddressExists ​ // dependency ​
​  getProductPrice ​ // dependency ​
​  createOrderAcknowledgmentLetter ​ // dependency ​
​  sendOrderAcknowledgment ​ // dependency ​
​  : PlaceOrderWorkflow = ​ // function definition ​
​  
​  ​ fun ​ unvalidatedOrder ->
​  ...

สิ่งนี้มีข้อดีเพิ่มเติม คือ workflow ทั้งหมดสามารถทดสอบได้ง่าย เพราะ dependency ทั้งหมดสามารถแทนที่ด้วยของปลอมได้

ในทางปฏิบัติ composition root function ควรอยู่ใกล้กับจุดเริ่มต้นของแอปพลิเคชันให้มากที่สุด—function main สำหรับ console app หรือ handler OnStartup/Application_Start สำหรับแอปที่รันต่อเนื่อง เช่น web service

นี่คือตัวอย่างของ composition root สำหรับ web service ที่ใช้ framework Suave [27] ก่อนอื่น service จะถูกตั้งค่า จากนั้น workflow จะได้รับ dependency ทั้งหมด และสุดท้าย routing จะถูกตั้งค่าเพื่อส่ง input ไปยัง workflow ที่ถูกต้อง:

​  ​ let ​ app : WebPart =
​  
​  ​ // set up the services used by the workflow ​
​  ​ let ​ checkProductExists = ...
​  ​ let ​ checkAddressExists = ...
​  ​ let ​ getProductPrice = ...
​  ​ let ​ createOrderAcknowledgmentLetter = ...
​  ​ let ​ sendOrderAcknowledgment = ...
​  ​ let ​ toHttpResponse = ...
​  
​  ​ // set up the "placeOrder" workflow ​
​  ​ // by partially applying the services to it ​
​  ​ let ​ placeOrder =
​  placeOrder
​  checkProductExists
​  checkAddressExists
​  getProductPrice
​  createOrderAcknowledgmentLetter
​  sendOrderAcknowledgment
​  
​  ​ // set up the other workflows ​
​  ​ let ​ changeOrder = ...
​  ​ let ​ cancelOrder = ...
​  
​  ​ // set up the routing ​
​  choose
​  [ POST >=> choose
​  [ path ​ "/placeOrder" ​
​  >=> deserializeOrder ​ // convert JSON to UnvalidatedOrder ​
​  >=> placeOrder ​ // do the workflow ​
​  >=> postEvents ​ // post the events onto queues ​
​  >=> toHttpResponse ​ // return 200/400/etc based on the output ​
​  path ​ "/changeOrder" ​
​  >=> ...
​  path ​ "/cancelOrder" ​
​  >=> ...
​  ]
​  ]

คุณจะเห็นว่าถ้า path เป็น /placeOrder เราจะเริ่มกระบวนการ “place order” โดยเริ่มจากการ deserialize input จากนั้นเรียก pipeline หลัก placeOrder แล้วจึง post event และแปลง output ให้เป็น HTTP response เราไม่มีพื้นที่พอที่จะพูดถึง function อื่นนอกจาก placeOrder แต่เทคนิค deserialization จะถูกพูดถึงใน บท Serialization .

Too Many Dependencies? (Dependency เยอะเกินไปไหม)

validateOrder มี dependency สองตัว แล้วถ้ามันต้องการสี่หรือห้าตัว หรือมากกว่านั้นล่ะ และถ้าขั้นตอนอื่นๆ ต้องการ dependency อีกมากมาย คุณอาจจะเจอปัญหา dependency ระเบิดจำนวน ถ้าเกิดเหตุการณ์นี้ขึ้น คุณควรทำยังไง

อย่างแรก อาจเป็นไปได้ว่า function ของคุณทำหลายอย่างเกินไป คุณสามารถแยกมันออกเป็นส่วนย่อยๆ ได้ไหม ถ้าทำไม่ได้ คุณอาจจะจัดกลุ่ม dependency ไว้ใน record structure เดียว แล้วส่งมันไปเป็น parameter เดียว

สถานการณ์ที่พบได้บ่อยคือเมื่อ dependency ของ child function เองก็ซับซ้อนมากเป็นพิเศษ ตัวอย่างเช่น สมมติว่า function checkAddressExists กำลังคุยกับ web service ที่ต้องการ URI endpoint และ credentials:

​  ​ let ​ checkAddressExists endPoint credentials =
​  ...

เราจะต้องส่ง parameter เพิ่มอีกสองตัวนี้ไปยังผู้เรียก toAddress ด้วยไหม แบบนี้?

​  ​ let ​ toAddress checkAddressExists endPoint credentials unvalidatedAddress =
​  ​ // only ^ needed ^ for checkAddressExists ​
​  
​  ​ // call the remote service ​
​  ​ let ​ checkedAddress = checkAddressExists endPoint credentials unvalidatedAddress
​  ​ // 2 extra parameters ^ passed in ^ ​
​  ...

แล้วเราก็จะต้องส่ง parameter เพิ่มเหล่านี้ไปยังผู้เรียก toAddress ด้วยเช่นกัน และเป็นแบบนี้ไปเรื่อยๆ จนถึงจุดบนสุด:

​  ​ let ​ validateOrder
​  checkProductExists
​  checkAddressExists
​  endPoint ​ // only needed for checkAddressExists ​
​  credentials ​ // only needed for checkAddressExists ​
​  unvalidatedOrder =
​  ...

ไม่ แน่นอนว่าเราไม่ควรทำแบบนี้ function ตัวกลางเหล่านี้ไม่ควรต้องรู้อะไรเลยเกี่ยวกับ dependency ของ function checkAddressExists แนวทางที่ดีกว่ามากคือการตั้งค่า low-level function ภายนอก function ระดับบนสุด แล้วส่ง child function ที่ประกอบไว้ล่วงหน้าพร้อม dependency ทั้งหมด bake ไว้แล้วเข้าไปแทน

ตัวอย่างเช่น ใน code ด้านล่าง เรา bake URI และ credentials เข้าไปใน function checkAddressExists ระหว่างการตั้งค่า เพื่อให้มันสามารถใช้เป็น function ที่มี parameter เดียวได้ตั้งแต่นั้นเป็นต้นไป function ที่ถูกทำให้ง่ายขึ้นนี้สามารถส่งเข้าไปได้ทุกที่เหมือนเดิม:

​  ​ let ​ placeOrder : PlaceOrderWorkflow =
​  
​  ​ // initialize information (e.g from configuration) ​
​  ​ let ​ endPoint = ...
​  ​ let ​ credentials = ...
​  
​  ​ // make a new version of checkAddressExists ​
​  ​ // with the credentials baked in ​
​  ​ let ​ checkAddressExists = checkAddressExists endPoint credentials
​  ​ // etc ​
​  
​  ​ // set up the steps in the workflow ​
​  ​ let ​ validateOrder =
​  validateOrder checkProductCodeExists checkAddressExists
​  ​ // the new checkAddressExists ^ ​
​  ​ // is a one parameter function ​
​  ​ // etc ​
​  
​  ​ // return the workflow function ​
​  ​ fun ​ unvalidatedOrder ->
​  ​ // compose the pipeline from the steps ​
​  ...

แนวทางการลด parameter โดยการส่ง helper function ที่ “ประกอบไว้ล่วงหน้า” เข้าไปนี้เป็นเทคนิคทั่วไปที่ช่วยซ่อนความซับซ้อน เมื่อ function หนึ่งถูกส่งเข้าไปใน function อีกตัว “interface”—นั่นคือ function type—ควรมีขนาดเล็กที่สุดเท่าที่จะทำได้ โดยซ่อน dependency ทั้งหมดไว้