Home >

Castle NHibernate Facility – Configuration

7. April 2009

Castle’s extensibility points provide you many ways to make your life easier. One and most widely used extensibility point is the facilities. By using facilities, you can integrate various other frameworks and technologies easily.

I am going to talk about NHibernate Integration Facility, which is currently lead by me. It was originally written by Hamilton Verissimo, and its current shape is more or less the same as what he wrote.

The purpose of this post is to get feedback and shape the documentation accordingly.

Now, after an historical introduction, lets talk about the facility’s purpose, and in as the first part of the documentation, the configuration.

The purpose
The purpose of this facility to provide an easy way to integrate NHibernate into MicroKernel backed applications. The facility provides a nice way to manage multiple session factories, a good way to manage sessions and it also provides some other structures that can be used to further integrate other frameworks such as Fluent NHibernate. It also plays nicely with other Castle services such as Transaction Management.


The Configuration
Currently, the facility can only be configured via XML, but it provides some extension points that can be used to configure it programmatically. The traditional way of configuring it is as given below. 

<configuration>
    <facilities>
        <facility
          id="nhibernatefacility"
          type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"
          [optional: configurationBuilder="Your custom configuration builder"] [optional: isWeb="Your custom configuration builder"]>
            <factory id="sessionFactory1">
                <settings>
                    <item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
                    <item key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
                    <item key="connection.connection_string">Data Source=.;Initial Catalog=test;Integrated Security=SSPI</item>
                    <item key="dialect">NHibernate.Dialect.MsSql2000Dialect</item>
                    <item key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</item>
                </settings>
                <assemblies>
                    <assembly>YourAssembly.Name.Here</assembly>
                </assemblies>
            </factory>
        </facility>
    </facilities>
</configuration>
<httpModules>
    <add name="NHibernateSessionWebModule"
             type="Castle.Facilities.NHibernateIntegration.Components.SessionWebModule, Castle.Facilities.NHibernateIntegration"/>
</httpModules>

isWeb: In case of web application, you should set this to true, implement IContainerAccessor in your HttpApplication (the global.asax) also add following lines to your <httpModules> section


As it can bee seen here, it is not very much different from the way we configure NHibernate. What happens behind the scenes are that: Facility creates an NHibernate Configuration, registers the SessionManager, and make those ready to be resolved.

The facility provides you some way to modify the Configuration that is created behind the scenes. The point is called IConfigurationContributor. By implementing classes that derive from that interface and registering them to container, you’ll be able to modify the NHibernate Configuration, just before the SessionFactory is resolved. The interface is simple

public interface IConfigurationContributor
{
    void Process(string name,Configuration config);
}

 

The name corresponds to the id in the configuration.

You can also Configure NHibernate Configuration via the IConfigurationBuilder interface. Currently, there are 2 built in ConfigurationBuilders, namely DefaultConfigurationBuilder and XmlConfigurationBuilder.
The default one is the one that parses the thing you see above while XmlConfigurationBuilder uses NH’s native xmls.

In the next post, I will hopefully show to integrate FluentNHibernate using custom ConfigurationBuilder

, ,

Comments

Valeriu Caraulean
Valeriu Caraulean
4/27/2009 7:40:25 AM #
Will be nice to have ability to declare the facility without any factory configuration.

Our use case: session factories created in runtime, kind of multi-tenant (multi-database) application.
At the time, only way we found was to override facility classs itself. We skipped some methods from initialization. Worked well, but I'd like a more "natural" way to do it...

PS not a real request, just a "feedback".
4/27/2009 8:33:09 AM #
Hi Valeriu,

You mean "having configuration" on the fly, then initializing the SessionFactory, right? If that is the case you might consider using IConfigurationBuilder interface.

In multitenant application, you may also consider using
nhforge.org/.../...-info-in-connection-string.aspx

8/9/2010 5:26:20 PM #
Pingback from maccork.com

Rhino Security : Part 1 – Setup/Configuration | macCork
11/20/2010 3:56:53 AM #
Hello People! Your blog is awesome! I would like to visit it everyday. It's very informative and amazing
12/22/2010 1:17:58 PM #
Thx for configuration. Your article is very helpful for people like me.
1/10/2011 3:50:06 AM #
Thanks for the configuration. REally helpful!
1/12/2011 2:01:15 AM #
Highly educative appreciate it, I do believe your current followers might probably want way more posts of this nature carry on the excellent hard work.
1/12/2011 2:41:34 AM #
Extremely interesting cheers, I reckon your trusty followers might possibly want more writing of this nature keep up the good work.
1/12/2011 5:17:55 AM #
Fantastically insightful many thanks, I presume your current followers could quite possibly want a great deal more blog posts like that maintain the good effort.
2/23/2011 2:55:30 PM #
Hi, I found your post really helpful. It helped me all the way in completing my assignment, I am also giving a referance link of your blog in my case study. Thanks for posting such informative content. Keep posting.
2/23/2011 3:16:33 PM #
Hi, The topic that you have discussed in the post is really amazing, I think now I have a strong hold over the topic after going through the post. I will surely come back for more information.
3/22/2011 12:47:41 AM #
Hey very nice blog!!
3/22/2011 6:09:07 PM #
Great researched information you've got here! Ill mention your site in my blog Smile
3/22/2011 11:27:54 PM #
Hi, how are you doing?  I thought that this post is cool.  The information contained in that writing has created inspiration inside me to the point where I'm reconsidering some things. I appreciate that.  I will be visiting this website in the future.  Please keep adding essays to this blog.  Your readers are waiting for the next writing.
3/23/2011 6:41:36 AM #
Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.
Comments are closed