Castle NHibernate Facility – Configuration

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>

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.

 
<httpModules>
    <add name="NHibernateSessionWebModule"
             type="Castle.Facilities.NHibernateIntegration.Components.SessionWebModule, Castle.Facilities.NHibernateIntegration"/>
</httpModules>


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


Posted 04-06-2009 3:56 PM by Tuna Toksoz
Filed under: ,

[Advertisement]

Comments

Paul Cowan wrote re: Castle NHibernate Facility – Configuration
on 04-07-2009 7:32 AM

I use Binsor to configur the Nhibernate facility.  So it is possible outside of Xml.

Tuna Toksoz wrote re: Castle NHibernate Facility – Configuration
on 04-07-2009 11:17 AM

Yes, you are right, I forgot about windsor.

Steve wrote re: Castle NHibernate Facility – Configuration
on 04-08-2009 12:34 AM

Thanks, I'm glad to see someone write about this - as I tend to handle this all 'manually'

That being said -- could you continue before moving to your next topic to expand just a bit more on the next few steps?

ie. so I have a repository, how does it get a hold of the session for, ie. a transaction ?  a criteria ?

If for the web, it will handle the session creation for me ?  

Thanks, good article, you left me wondering how to actually implement it  :)

Tuna Toksoz wrote re: Castle NHibernate Facility – Configuration
on 04-08-2009 1:48 AM

That will be the third post, probably. I thought  about covering configuration, and also include one of the extensibility points.

Stuart Laughlin wrote re: Castle NHibernate Facility – Configuration
on 04-09-2009 6:47 PM

IConfigurationContributor is exactly what I was looking for today, and I found it thanks to you via this article.

How did I ever get anything done before I started using Windsor??? :)

--Stuart

Tuna Toksoz wrote re: Castle NHibernate Facility – Configuration
on 04-10-2009 2:21 AM

Hello Stuart,

Can you tell me for what reason are you using IConfigurationContributor? In the next article, I will touch to IConfigurationBuilders and IConfigurationContributors so it is nice to hear your case.

Thanks!

ged wrote re: Castle NHibernate Facility – Configuration
on 05-24-2009 1:45 PM

does anyone know where i can get the Castle stck that is binary compatible with NHibernate-2.1.0

Tuna Toksoz wrote re: Castle NHibernate Facility – Configuration
on 05-24-2009 2:02 PM

For NHFacility, you'll have to use SVN for now, until I release (which I believe in June)

For Active Record, IIRC, there is a release using NH 2.1 but not a GA.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)