views:

1071

answers:

2

I'm checking out Linq for NHibernate 2.1 to use in an ASP.NET MVC application and I am trying to figure out session management. As a first experiment I am trying to modify the SportsStore application from Pro ASP.NET MVC Framework. This sample app uses Linq to Sql so I thought it would be a good exercise. I have NHibernate 2.1 accessing the database and non-Linq queries work fine.

In Linq to SQL we can create an IQueryable object from a data context and a connection string that can be passed around and used without worrrying about maintaining any context. So for example, the original product repository code reads something like:

public IQueryable<Product> Products
{
 get { (new DataContext(_connectionString)).GetTable<Product>();
}

and the app uses the returned IQueryable to populate product lists, retrieve product details, etc.

In NHibernate Linq similarly uses a data context but instead of a connection string, we supply a session object that has to remain in scope while the IQueryable is in use:

IQueryable<Product> products = new SportsStoreContext(session).Products;

So I'm wondering how I'm supposed to manage that session without doing too much violence to the structure of the sample application. Ideally I'd like a solution that would enable me to replace Linq to Sql code with NHibernate Linq by making only local changes.

An NHibernate session implements IDisposable, and most NHibernate samples demonstrate session management with a using{} construct but this strategy wouldn't work here. This thread discusses some different approaches, the S#arp Architecture and the HybridSessionBuilder. Is anyone using these for to replace Linq to Sql with NHibernate Linq? Are there other methods that would work better?

**EDIT mausch has it right - the common way to approach this is with a httpmodule. NHibernate in Action describes two ways to do this: session-per-request (p334) and session-per-conversation (p340). The latter allows you to reuse persistent instances across multiple http requests. In addition to the links mausch provided, NHibernate in Action mentions NHibernate Burrow, Castle ActiveRecord, and Rhino Tools.

+4  A: 

IMO the easiest way would be having a httpmodule that manages the session (session-per-request pattern) and then using dependency injection to supply the ISession to the controllers.

This is pretty much the approach used by S#arp and others.

LINQ for NHibernate builds on top of the "standard" NHibernate, so all session management patterns still apply.

Don't have the book though so I don't know how much would have to be changed...

Mauricio Scheffer
Right you are mausch. I'm experimenting with code plus reading NHibernate in Action. I just got through Chapter 10 (Architectural patterns for persistence) where they describe this pattern too.
Keith Morgan
A: 

Rhino Commons is a solution too. I use it in an ASP.NET MVC application works fine.

Kris-I
In Rhino Commons is some code to have multiple simultaneous sessions going if your webapp is really ajaxy. The code allows for a session token to be passed with each request so that you can have the same user doing different transactions at once. Crazyness.
Will Shaver