2011-04-24 91 views
0

我現在正在研究F#的Entity Framework Code First Wrapper,我一直在想我是否應該將所有模塊合併到一個模塊中。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 

回答

7

這是一個棘手的問題設計。

通常,F#庫傾向於與在名爲一個單獨的模塊類型(例如list<T>seq<T>)工作相同的類型(例如List.xyzSeq.xyz)組的功能。這適用於常見類型,因爲用戶可以學習如何查找功能。但是,對於較不常見的類型(例如,您自己的庫),發現這些模塊可能有點困難(特別是如果類型不是顯式創建的(但是由於某個調用而獲得)模塊名稱,然後用.去探索它是相當強大的

我可能會建議使用嵌套的模塊是這樣的:。

module EF = 
    // common functions 
    module Cascadable = 
    // CascadableNavProperty functions 
    module Many = 
    // etc. 

的用戶可以再找個用EF開始一切,他們會寫例如:

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