F# 模块还是类?

F# 模块还是类?,f#,functional-programming,F#,Functional Programming,考虑到这两种方法: 方法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

考虑到这两种方法:

方法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)
哪一个最适合函数式编程?我假设
方法1
会,但是每次调用
DomainCRUD.GetWhere collection
感觉有点多余


哪一种是最灵活、最“易读”的方法1,原因如下:

  • 与类关联的函数不是,但与模块关联的函数是。这意味着您可以在模块内部分应用函数,以实现在OO代码中通常使用DI框架完成的功能。(见丹尼尔的评论)
  • 只需通过
    打开DomainCRUD
    即可省略模块限定
    DomainCRUD.GetWhere
  • 除了打开模块外,还可以使用或(相反)标记模块,这提供了类所不具备的额外灵活性
  • 基于模块的方法不那么冗长

  • 如果你打算使用所有来自F#only的方法,我会说方法1。如果您想查看典型的F#程序结构,可以检查或其他。例如(在Deedle中),在帧上有和包含大多数操作。几乎没有必要复制#1。类可以有curry成员函数。我不知道!谢谢你的提示。