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

  • Home
  • SEARCH
  • 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 8314521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:50:50+00:00 2026-06-08T20:50:50+00:00

I’m trying to set NHibernate to handle my sessions per web request. I have

  • 0

I’m trying to set NHibernate to handle my sessions per web request. I have a project that initializes my Session when any of my repositories are used. I would like to know the best way to manage my session per each web request. Because my architecture is decoupled, I would like to avoid Opening the session and closing the session in my web project. Is there a good way to handle this without requiring the web project to know about the existence of NHibernate?

This is my request flow:

Web Request -> ASP.NET MVC 3 -> Controller -> Service -> Repository (Opens Session) -> NHibernate -> Database -> Back to MVC and out.

  • 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-06-08T20:50:51+00:00Added an answer on June 8, 2026 at 8:50 pm

    Have a look at S#arp Architecture. It’s a framework that wraps NHibernate and provides this for you, as well as some other features. You can also do this with a dependency injection container like Castle Windsor or Ninject. (S#arp uses Castle Windsor).

    If you want to do this on your own, you would need to create an HttpModule to open and close the session for you at the start and end of each web request. At the start of a request, the module would open a session, and stash it away in the HttpContext.Items, which is per-web-request storage. Your repositories would get the session from here while processing the request. (For a clean design, create an interface ISessionManager that the repositories use, and an implementation that accesses the per-request storage. Then it’s unit-testable.) Finally the module would flush and close the session at the end of a request. I did this once on an NHibernate project, and though it was educational, it was a lot of work.

    S#arp is a large framework that dictates a lot of the application structure. If you don’t want to go that far, look at Castle Windsor to cleanly abstract this away for you. All you need to do (more or less) is configure the container to instantiate the repositories with a per-request NHibernate session object as a constructor parameter. If you really want to learn how, build your own HttpModule, but I recommend using a framework in production.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a view passing on information from a database: def serve_article(request, id): served_article
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.