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.