title: NHibernate Linq Session Management
body: <p>I'm checking out <a href="https://sourceforge.net/projects/nhibernate/files/NHibernate/2.1.0.GA/NHibernate.Linq-1.0.0.GA-bin.zip/download" rel="nofollow noreferrer">Linq for NHibernate 2.1</a> 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 <a href="http://www.apress.com/book/view/1430210079" rel="nofollow noreferrer">Pro ASP.NET MVC Framework</a>. 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.</p>
  
  <p>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:</p>
  
  <pre><code>public IQueryable&lt;Product&gt; Products
  {
      get { (new DataContext(_connectionString)).GetTable&lt;Product&gt;();
  }
  </code></pre>
  
  <p>and the app uses the returned IQueryable to populate product lists, retrieve product details, etc.</p>
  
  <p>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:</p>
  
  <pre><code>IQueryable&lt;Product&gt; products = new SportsStoreContext(session).Products;
  </code></pre>
  
  <p>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.</p>
  
  <p>An NHibernate session implements IDisposable, and most NHibernate samples demonstrate session management with a using{} construct but this strategy wouldn't work here. <a href="https://stackoverflow.com/questions/364540/nhibernate-session-management-in-asp-net-mvc">This thread</a> discusses some different approaches, the <a href="http://code.google.com/p/sharp-architecture/" rel="nofollow noreferrer">S#arp Architecture</a> and the <a href="http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/" rel="nofollow noreferrer">HybridSessionBuilder</a>. Is anyone using these for to replace Linq to Sql with NHibernate Linq? Are there other methods that would work better?</p>
  
  <p>**EDIT 
  mausch has it right - the common way to approach this is with a httpmodule. <a href="http://www.manning.com/kuate/" rel="nofollow noreferrer">NHibernate in Action</a> 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 <a href="http://sourceforge.net/projects/nhcontrib/files/NHibernate.Burrow/1.0.2.GA/NHibernate.Burrow-1.0.2.GA-bin.zip/download" rel="nofollow noreferrer">NHibernate Burrow</a>, <a href="http://www.castleproject.org/activerecord/index.html" rel="nofollow noreferrer">Castle ActiveRecord</a>, and <a href="http://rhino-tools.sourceforge.net/" rel="nofollow noreferrer">Rhino Tools</a>.</p>