F# - 合并模块?
我目前正在为 F# 开发一个实体框架代码优先包装器,我一直在想是否应该将所有模块合并为一个。
看看这个:
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
这需要我调用这样的代码:
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
使用这么多模块会让用户感到困惑吗? - 我应该将它们全部放入一个模块中,还是会破坏用户的概览?
所有函数都放置在同一模块中的示例:
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
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个棘手的设计问题。
通常,F# 库倾向于将与类型一起使用的函数(例如
list
或seq
)分组到名称与类型相同的单独模块中(例如List.xyz
和Seq.xyz
)。这对于常见类型非常有效,因为用户学习如何查找函数。然而,对于不太常见的类型(例如您自己的库),发现这些模块可能有点困难(特别是如果类型不是显式创建的(而是作为某些调用的结果获得的)。拥有一个模块名称,然后使用
.
来探索它是非常强大的。我可能会建议使用嵌套模块:
然后用户可以从
EF
开始找到所有内容。喜欢:This is a tricky design question.
Generally, F# libraries tend to group functions that work with a type (e.g.
list<T>
orseq<T>
) in a separate module with the name same as the type (e.g.List.xyz
andSeq.xyz
). This works well for common types, because the users learn how to find the functions.However, for less common types (e.g. 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:
The users could then find everything by starting with
EF
. They'd write something like: