[英]F# - Merge modules?
I'm at the moment working on a Entity Framework Code First Wrapper for F#, and I've been wondering whether I should merge all my modules into just one. 目前,我正在为F#开发Entity Framework Code First Wrapper,而我一直在想是否应该将所有模块合并为一个模块。
Take a look at this: 看看这个:
module ManyNavProperty =
let withMany (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithMany()
let withSeq expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithSeq expr
let withList expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithList expr
let withArray expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithArray expr
let withOptional (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithOptional()
let withOptionalProperty expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithOptional expr
let withRequired (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithRequired()
let withRequiredProperty expr (cfg:ManyNavPropertyInfo<'a,'b>) = cfg.WithRequiredProperty expr
module DependentNavProperty =
let hasForeignKey expr (cfg:DependentNavPropertyInfo<'a>) = cfg.HasForeignKey expr
module CascadableNavProperty =
let willCascadeOnDelete b (cfg:CascadableNavPropertyInfo) = cfg.WillCascadeOnDelete b
module EF =
let entity<'a when 'a : not struct> (cfg:DbModelBuilder) = EntityInfo<'a>(cfg.Entity<'a>())
let hasKey expr (cfg:EntityInfo<'a>) = cfg.HasKey expr
let hasSeq expr (cfg:EntityInfo<'a>) = cfg.HasSeq expr
let hasList expr (cfg:EntityInfo<'a>) = cfg.HasList expr
let hasArray expr (cfg:EntityInfo<'a>) = cfg.HasArray expr
let hasOptional expr (cfg:EntityInfo<'a>) = cfg.HasOptional expr
let hasRequired expr (cfg:EntityInfo<'a>) = cfg.HasRequried expr
let toTable s (cfg:EntityInfo<'a>) = cfg.ToTable s
that would require me to call the code like: 那将需要我像下面这样调用代码:
override x.OnModelCreating (mb:DbModelBuilder) =
let finished = ignore
let entity = EF.entity<Author> mb
entity
|> EF.hasSeq <@ fun z -> z.Books @>
|> ManyNavProperty.withRequiredProperty <@ fun z -> z.Author @>
|> DependentNavProperty.hasForeignKey <@ fun z -> z.AuthorId @>
|> CascadableNavProperty.willCascadeOnDelete true
|> finished
Is the use of so many modules confusing for the user? 这么多模块的使用是否会使用户感到困惑? - Should I place them all into one module, or will that destroy the overview for the user?
-我应该将它们全部放在一个模块中,还是会破坏用户的概览?
An example where all functions are placed in same Module: 所有功能都放在同一模块中的示例:
override x.OnModelCreating (mb:DbModelBuilder) =
let finished = ignore
let entity = EF.entity<Author> mb
entity
|> EF.hasSeq <@ fun z -> z.Books @>
|> EF.withRequiredProperty <@ fun z -> z.Author @>
|> EF.hasForeignKey <@ fun z -> z.AuthorId @>
|> EF.willCascadeOnDelete true
|> finished
This is a tricky design question. 这是一个棘手的设计问题。
Generally, F# libraries tend to group functions that work with a type (eg list<T>
or seq<T>
) in a separate module with the name same as the type (eg List.xyz
and Seq.xyz
). 通常,F#库倾向于将与类型(例如
list<T>
或seq<T>
)一起使用的函数分组在名称与类型相同的单独模块中(例如List.xyz
和Seq.xyz
)。 This works well for common types, because the users learn how to find the functions. 这对于常见类型非常有效,因为用户可以学习如何查找功能。
However, for less common types (eg your own library), it may be a bit difficult to discover these modules (especially if the type is not created explicitly (but is obtained as a result of some call). Having one module name and then use .
to explore it is quite powerful. 但是,对于不太常见的类型(例如,您自己的库),发现这些模块可能会有点困难(尤其是如果类型不是显式创建的(而是通过调用获得的))。使用
.
探索它是非常强大的。
I would probably suggest using nested modules. 我可能会建议使用嵌套模块。 Something like:
就像是:
module EF =
// common functions
module Cascadable =
// CascadableNavProperty functions
module Many =
// etc.
The users could then find everything by starting with EF
. 然后,用户可以从
EF
开始找到所有内容。 They'd write something like: 他们会写类似:
entity
|> EF.hasSeq <@ fun z -> z.Books @>
|> EF.Many.withRequiredProperty <@ fun z -> z.Author @>
|> EF.Dependent.hasForeignKey <@ fun z -> z.AuthorId @>
|> EF.Cascadable.willCascadeOnDelete true
|> finished
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.