Tuesday, August 04, 2009

Using Spring.Net Proxy Factory with NHibernate

The latest version of NHibernate (2.1) discouples the framework from the dependency of relying on Castle (think DynamicProxy) therefore allowing you to plug in any framework for you are proxying.

Overview: NHibernate uses proxies to facilitate lazy loading, this proxy mechanism allows NHibernate to intercept the calls to your properties and classes thus delaying a call to the database that may not be needed (Lazy Loading).

After you have downloaded NHibernate 2.1 and extracted the contents you will notice a new folder titled "Required_For_LazyLoading". Inside this folder are 3 separate folders containing binaries for the following proxies: Castle: LinFu & Spring. Since I'm using Spring framework in the rest of my project this allows me to have one less dependency (which is a good thing). As I understand it LinFu is the fastest proxy of the bunch so if you aren't using Castle or Spring already you may want to consider it.

The problem is that the NHibernate is distributed with an older verison of Spring so we want to tell the .Net framework that it's okay to use the new version. To do so were are going to use Assembly Redirection in our App.config or Web.config. The section below belongs inside your <configuration> section: