Search code examples
f#fluent-nhibernatefluent-interface

Best way to de-fluent an API?


I'm extending Fluent NHibernate for better use with F# (namely, quotation support), and want some feedback on de-fluenting the API. F# requires that return values be used, unless they are type unit. So this ends up terminating every line with "|> ignore":

type ProductMap() as m = inherit QClassMap<Product>() do
    let x = Unchecked.defaultof<Product> 
    m.Id <@ x.Id @> |> ignore
    m.Map <@ x.Name @> |> ignore
    m.Map <@ x.Price @> |> ignore
    (m.HasManyToMany <@ seq x.StoresStockedIn @>)
        .Cascade.All()
        .Inverse()
        .WithTableName("StoreProduct") |> ignore

My first reaction was to add more methods to the base class so they return unit. For instance, "IdI" and "MapI":

...
m.IdI <@ x.Id @>
m.MapI <@ x.Name @> 
m.MapI <@ x.Price @> 
... 

But that requires specific overloads here and there, and long chains are still going to need a |> Ignore. I also considered extending object with a Done property:

(m.Id <@ x.Id @>).Done
(m.Map <@ x.Name @>).Done
(m.Map <@ x.Price @>).Done
(m.HasManyToMany <@ seq x.StoresStockedIn @>)
    .Cascade.All()
    .Inverse()
    .WithTableName("StoreProduct").Done

What do you think?


Solution

  • IMHO a better approach would be to start from scratch thinking in F# (e.g function piping, currying, combinators) instead of wrapping fluent nhibernate, but using what fluent nhibernate has used to generate the mappings. That is, building a "parallel fluent nhibernate" for exclusive use of F#.

    I've recently posted about a similar issue with Windsor's fluent interface in F#. My conclusion is that many DSLs / fluent interfaces built for C# / VB.NET will break in F# so I think it's best to build specific fluent interfaces that suit the F# way.