Home >

Fluent Interface for NH Facility – Take 1

27. June 2009

We’ve been getting many requests on having fluent configuration for NHibernate Integration Facility, and as I like programmatic configuration more than XML configuration (did I mention that I hate XML?), I decided to work on it. After 2-3 hours, I got the below more or less working

container.Register(
Fluently.ConfigureFacility()
    .Id("nhibernateFacility")
    .DefaultConfigurationBuilder<DefaultConfigurationBuilder>()
    .DefaultConfigurationPersister<DefaultConfigurationPersister>()
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias("myAlias")
                        .Id("myId")
                        .UsingConfiguration(
                        FactoryConfigurator.DefaultBuilder()
                            .ConnectionProvider("…………………………")
                            .ConnectionDriver("…………………………")
                            .ConnectionString("…………………………")
                            .Dialect("…………………………")
                            .ProxyFactory("…………………………")
                            .Assemblies("…………………………")))
    .AddFactory(
                    Fluently.ConfigureFactory()
                        .Alias("myAlias")
                        .Id("myId")
                        .UsingConfiguration(
                        FactoryConfigurator.XmlBuilder().File("myFile.xml"))));

 

The pieces in Italic are not necessary to write as they have their defaults, also we have some generic overloads for things like Dialect, ProxyFactory and ConnectionProvider.

What the above interface does is that it actually converts all the above configuration to IConfiguration and add them to the container as Facility Configuration, this is actually what is done behind the scenes when you use XML configuration.

I asked for a review over the syntax from several tweeps (@kkozmic, @dagda1, @mikehadlow, @chriscanal) and from one other NH Facility user, German Schuager, and got great feedback.

One of the issues that they pointed out with the above interface is that it is less discoverable. You have to find Fluently class, and then FactoryConfigurator class. Another issue is that it feels less natural to configure the facility like this. Instead, they prefer the configuration take place right on the facility.

This one is what German Schuager has suggested

container.AddFacility<NHibernateFacility>("nhibernateFacility", cfg => cfg
        .DefaultConfigurationBuilder<DefaultConfigurationBuilder>()
        .DefaultConfigurationPersister<DefaultConfigurationPersister>()
        .AddFactory("id1", f => f
                .Alias("myAlias")
                .UsingConfiguration<DefaultBuilder>(c => c
                        .ConnectionProvider("………")
                        .ConnectionDriver("………")
                        .ConnectionString("………")
                        .Dialect("………")
                        .ProxyFactory("………")
                        .Assemblies("………")
                ))
        .AddFactory("id2", f => f
                .Alias("alias")
                .UsingConfiguration<XmlBuilder>(c => c
                        .ReadFrom("myfile.xml")
                ))
 });

and similar one from Krzysztof Kozmic.

Looks like i was the only one that is fan of Castle Microkernel style Fluent Interface.

Let’s see what I’ll come up for the second take, If I ever do it.

Comments

6/27/2009 3:24:17 PM #
I use and have been using Binsor to configure my castle facilities including nhf for a while now that is another option available if xml configuration is not your thing.  I don't think you can configure more than one session factory with binsor although I could be wrong.

With the explosion fluent interfaces go, I personally find lamda based chaining totally undiscoverable and I ten.  

I have seen some seriously horrible multi-line lamda expressions that only the author could trully know.
6/29/2009 2:45:47 PM #
When I did the AutoGen stuff, I tried to keep it as consistent with the existing castle stuff, but ended up moving towards the Action<Settings> model you've got in the second example.  The former looks more extensible, but in practice (and very much in my opinion) it tends to be illusory, the latter syntax has incredibly locked down scope, which seems to work out better.  And it's more discoverable (providing you're familiar with the convention.).

I notice you've used the class name "Fluently" in the first example, which is also used by FNH.  Don't know if that's likely to prove a problem in future.
9/4/2009 6:33:55 PM #
Would it be possible to build a session factory via FluentNH, then pass that to the facility?  That would be awesome.
9/4/2009 6:39:24 PM #
Yes it is very possible currently. Check IConfigurationBuilder

mikehadlow.blogspot.com/.../...nhibernate-and.html
Comments are closed