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/28/2009 12:24:17 AM #
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 11: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/5/2009 3:33:55 AM #
Would it be possible to build a session factory via FluentNH, then pass that to the facility?  That would be awesome.
9/5/2009 3:39:24 AM #
Yes it is very possible currently. Check IConfigurationBuilder

mikehadlow.blogspot.com/.../...nhibernate-and.html
11/20/2010 3:54:43 AM #
Hello People! Your blog is awesome! I would like to visit it everyday. It's very informative and amazing
12/27/2010 7:36:45 AM #
I teach a university[url=www.dvdtopshop.com/.../...64-discs-dvd-boxset.html]walt disney dvd[/url] course in popular culture. We read a book about Walt Disney, and I bought True Life Adventures just to show the class. I did not expect to see for fun. What a mistake. It's incredible. Today we have[url=www.dvdtopshop.com/.../...sons-1-6-dvd-boxset.html]grey's anatomy dvd[/url] Animal Planet and other educational networks, but Disney documentary is so much better from the point of view of entertainment. The landscape just brings oh and ah, and humor and intrigue, then maybe boring [url=www.dvdtopshop.com/.../...sons-1-9-dvd-boxset.html]smallville dvd[/url]for scientists to mind, just the flow of stories.What I really liked was that you could see La Prairie leak, then the disc 2 is the segment of the wonderful world of Disney and color, where you can see how he used TV [url=www.dvdtopshop.com/.../...sons-1-6-dvd-boxset.html]house md dvd[/url]market Journal filmmakers to film where you get some great behind the scenes pictures of how the film was made. (Expect to see the snow-mobile!)I [url=www.dvdtopshop.com/.../...ons-1-5-dvd-boxset-.html]the closer dvd[/url]bought this for the class. I get all the others in the series for fun.
1/10/2011 3:59:29 AM #
I'm also a fan of Castle Microkernel style Fluent Interface! Smile
1/11/2011 1:07:39 PM #
Really like this website, this really helps and very useful.
2/23/2011 12:37:05 PM #
I find posts on this topic and I was fortunate to find yours.I to send this site to my friends so they can read and complete our project.hope to get some good marks in this project.Thank. You for this awesome post.
3/22/2011 8:25:05 PM #
I actually really enjoy the details this specific online site continuously gives. Top shelf!
Comments are closed