鉴于这两种方法:
方法1
module DomainCRUD =
let getWhere collection cond = ...
module DomainService =
let getByCustomerId f customerId =
f(fun z -> z.CustomerId = customerId)
// USAGE:
let customerDomains = DomainCRUD.getWhere collection
|> DomainService.getByCustomerId customerId
方法2
type DomainCRUD(collection) =
member x.GetWhere cond = ...
type DomainService(CRUD) =
member x.GetByCustomerId customerId =
CRUD.GetWhere(fun z -> z.CustomerId = customerId)
// USAGE:
let domainService = new DomainService(new DomainCRUD(collection))
let customerDomains = _domainService.GetByCustomerId(customerId)
哪种功能最适合函数式编程?我假设approach 1
会,但每次调用DomainCRUD.GetWhere collection
感觉有点多余。
哪个最灵活,最“可读”?
答案 0 :(得分:3)
方法1,原因如下:
DomainCRUD.GetWhere
。open DomainCrud
[<AutoOpen>]
或(相反)[<RequireQualifiedAccess>]
进行标记,这样可以提供课程无法提供的额外灵活性。