Make NHibernate and Enterprise Library play nice together

Make NHibernate and Enterprise Library play nice together

Recently we have been introducing NHibernate and a domain layer to an older system that relies primarily on the Enterprise Library Data Access application block and stored procedures for database access.

This has mostly gone pretty smoothly, except in situations where we mix the two strategies inside a transaction. Enterprise Library and NHibernate both manage their own connections and if we try to wrap them both in a TransactionScope it gets promotes to a distributed transaction, causing all sorts of headaches. Wouldn’t it be great if NHibernate and Enterprise Library could just share a single SqlConnection instead?

Luckily NHibernate makes this sort of thing really easy to achieve. All you need to do is write a custom IConnectionProvider that wraps Enterprise Library’s Database.CreateConnection(). Then just drop into your hibernate.xml.cfg:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">  <session-factory>    <property name="connection.provider">Xyz.EntLibConnectionProvider, Xyz</property>    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Spring.ProxyFactoryFactory, NHibernate.ByteCode.Spring</property>  </session-factory></hibernate-configuration>

Note you don’t need a connection.connection_string anymore because it retrieves it from the Enterprise Library’s dataConfiguration section:

<dataConfiguration defaultDatabase="XyzProd">  <connectionStrings>    <add name="XyzProd"         providerName="System.Data.SqlClient"            connectionString="server=localhost; database=AdventureWorks; UID=user;PWD=word;" />  </connectionStrings></dataConfiguration>

You can grab the code here: EntLibConnectionProvider.cs