Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 298301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:43:08+00:00 2026-05-12T06:43:08+00:00

I’m checking out Linq for NHibernate 2.1 to use in an ASP.NET MVC application

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:43:09+00:00Added an answer on May 12, 2026 at 6:43 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am confused How to use looping for Json response Array in another Array.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.