Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Billy McCafferty



Using Spring.NET and NHibernate with ASP.NET MVC

[Update 6/11/08 - To see an example of this in action, read the introduction of S#arp Architecture.] 

I've been quiet lately as I've been a very busy bee preparing the inner workings of the sequel to my NHibernate Best Practices article.  In the next installment, I'll be discussing in detail how to leverage the best of Spring.NET for dependency injection and transaction management, the best of NHibernate for a nearly touch-free data access layer and non-intrusive custom collections, and ASP.NET MVC with the MvcContrib project for a bomber presentation tier.

The biggest challenge I've faced in one sitting is getting NHibernate lazy loading to work in symbiosis with Spring.NET dependency injection for controllers.  The key is to leverage Spring's WebApplicationContext instead of trying to create and configure your own within Application_Start in Global.asax.cs.  The steps to realize this are as follows:

  1. Configure Spring.NET as you normally would for an ASP.NET application, but leave out the PageHandlerFactory setting.
  2. Configure Spring.NET for your controllers.  In the sample below, a data access object is being passed using constructor injection to the controller.
  3. Instruct Global.asax.cs to leverage the loaded Spring.NET context when initializing the MvcContrib project's Spring.NET IoC controller factory.  The crux of the problem here is that you can't get a handle on the web application context in Application_Start as it's only available after all of the HttpModules (e.g., Spring.NET's WebSupportModule) have been loaded into memory.  Therefore, you must wait until HttpApplication.Init() to get a hold of the context loaded via the web.config file. The following code, which leverages MvcContrib's Spring.NET controller factory, demonstrates how to do this.

With the above in place, you can now leverage Spring.NET's dependency injection, transaction management, and the open-session-in-view pattern for NHibernate session management and lazy loading within your views.  There are obviously details left out, but if you've been working on this problem, this is the concise solution.  (This post uses Spring.NET 1.1, NHibernate 1.2, ASP.NET MVC Preview 2, and MvcContrib 0.0.1.96 Beta)

Billy McCafferty



Comments

Steve Gentile said:

Interesting topic - I spent half my day today building a Northwind sample that uses the spring.net 1.1, nhibernate 1.2.1 and MS MVC  :)

I'm injecting the Dao into the controller as well  :)

Spring.NET  + NHibernate is a great setup!

I'd like to figure out how to use the new NHibernate 2.0 with Ayende's NHibernate over Linq, but I'm concerned the spring wouldn't handle it.

# April 4, 2008 8:26 PM

Billy McCafferty said:

My thoughts exactly!

# April 4, 2008 8:44 PM

Vlad Navazhylau said:

Can't wait for full article! Thanks!

# April 5, 2008 3:29 PM

Links Today (2008-04-06) said:

Pingback from  Links Today (2008-04-06)

# April 6, 2008 11:33 AM

Reflective Perspective - Chris Alcock » The Morning Brew #67 said:

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #67

# April 7, 2008 3:07 AM

Dew Drop - April 7, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - April 7, 2008 | Alvin Ashcraft's Morning Dew

# April 7, 2008 8:03 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 8, 2008 10:38 AM

Nick said:

Regarding Linq to NHibernate, this is just a query layer on top of NH - you can go ahead and integrate NH the normal way.

Your repositories may expose or use an IQueryable<T>, and things will just work as usual.

Not 100% sure about building Linq to NH with NH 2.0, but I'd say its possible.

Nick

# April 8, 2008 7:06 PM

Mark Pollack said:

Hi Billy,

I've always enjoyed your articles and I'm certainly looking forward to this one.  Let me know if there is anything I can do to be of help.

Note that an open issue is to provide separate exception translation advice to convert NHibernate exceptions to Spring's DAO exception hierarchy. This way you can get all the benefits of HibernateTemplate but using the standard NHibernate API.  This already exists in Spring.Java, some details are here - blog.springsource.com/.../so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate

Cheers,

Mark Pollack (Spring.NET guy... ;)

# April 9, 2008 10:28 AM

Wöchentliche Rundablage: Silverlight 2, ASP.NET MVC, .NET 3.5, jQuery, CSS, Powershell | Code-Inside Blog said:

Pingback from  W&ouml;chentliche Rundablage: Silverlight 2, ASP.NET MVC, .NET 3.5, jQuery, CSS, Powershell | Code-Inside Blog

# April 14, 2008 8:30 AM

Weekly Links: Silverlight 2, ASP.NET MVC, .NET 3.5, jQuery, CSS, Powershell | Code-Inside Blog International said:

Pingback from  Weekly Links: Silverlight 2, ASP.NET MVC, .NET 3.5, jQuery, CSS, Powershell | Code-Inside Blog International

# April 14, 2008 8:39 AM

Jmosco said:

Do you have the project in a zip file?

this instructions works in VS 2003?

Thanks

# April 17, 2008 6:32 PM

Billy McCafferty said:

Honestly, I'm not sure if this will work in VS 2003...I don't think it will.  I *believe* that it's dependent on .NET 2.0, but it *may* work with .NET 1.1...not sure though.  My example project definitely wouldn't work in VS 2003 though...you'd have to upgrade to VS 2008.

# April 17, 2008 8:16 PM

jmosco said:

Hi Billy, i implemeted Spring .Net and NHibernate in VS 2003 but i can use Hibernate Template, do you have some suggestions for this.

I like how it works, but i would like work with NHibernate template.

See you.

# May 13, 2008 9:26 AM

ravelo said:

Hi Billy, I've used a Nhibernet with spring.Net, on the test an exception was standing, and an error message was dispalaying as "class unkwon" .

Could you give me a solution please

# May 14, 2008 4:26 AM

Billy McCafferty said:

Make sure that the HBM is compiled as an "Embedded Resource" in its properties.

# May 15, 2008 12:34 PM

Billy McCafferty said:

@jmosco,

I'll be posting a fully working sample later today.

# May 15, 2008 12:42 PM

Cialis best price buy online. said:

Cialis online. Cialis best price buy online.

# July 16, 2008 10:41 PM

ASP.NET MVC Archived Buzz, Page 1 said:

Pingback from  ASP.NET MVC Archived Buzz, Page 1

# July 28, 2008 4:39 PM

ASP Net Web Development said:

Ver few articles are available on using Spring.NET and NHibernate with ASP.NET MVC... great explanation thank you...

# September 1, 2008 8:05 AM

Casino said:

The New Dress summary and study guide, with notes, essays, quotes, and pictures.

# September 23, 2008 3:57 PM

Poker said:

Information about cell phones for the US Detailed info on new phones, news

# September 23, 2008 7:41 PM

Casino said:

The New Dress summary and study guide, with notes, essays, quotes, and pictures.

# September 23, 2008 7:42 PM

BLACKJACK said:

Get the latest mobile phones online at Dialaphone, the mobile phone shop offering great deals on the latest mobile phones.

# September 23, 2008 7:43 PM

BLACKJACK said:

Get the latest mobile phones online at Dialaphone, the mobile phone shop offering great deals on the latest mobile phones.

# September 23, 2008 7:44 PM

DotNetShoutout said:

Your Story is Submitted - Trackback from DotNetShoutout

# November 20, 2008 5:04 AM

Sean Gilbert said:

ADO.NET Entity Framework with Oracle

# November 20, 2008 2:50 PM

grim said:

Hey, Billy. I try today use your sample but that do not work for me.

MVC IControllerFactory  have default implement and after each request they always create new instance of requested controller, in spring framework we always have singleton version. And in that case on post request we always have data witch sended in first post request

sorry for my poor english

# November 25, 2008 9:52 AM

Billy McCafferty said:

Hi grim,

As you noted, each controller is created upon every request.  In the earlier versions, but you could provide your own controller factory, instead of using the one within the framework to ensure that controllers follow the singleton pattern.

# November 25, 2008 10:11 AM

grim said:

Thanx for fast answer.

I work at application which using Spring.NET and yes, now  i try implement my own factory which be support that IoC framework but i cannot find object in controller class which need cleansing

my goal is make singleton controller with all DJ looks like first time created

can you help me with that?

my mail is max.itcn@gmail.com

# November 25, 2008 10:37 AM

Billy McCafferty said:

If you're using Ninject, you can modify your custom controller factory to create controllers as singletons instead of being transient.  Here's a webcast concerning this capability:  dimecasts.net/.../38

# November 25, 2008 10:50 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Proudly Partnered With