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 772085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:45:21+00:00 2026-05-14T18:45:21+00:00

Does anyone have any tips or best practices regarding how Autofac can help manage

  • 0

Does anyone have any tips or best practices regarding how Autofac can help manage the NHibernate ISession Instance (in the case of an ASP.NET MVC application)?

  • 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-14T18:45:21+00:00Added an answer on May 14, 2026 at 6:45 pm

    I’m not overly familiar with how NHibernate sessions should be handled. That said, Autofac have excellent instance lifetime handling (scoping and deterministic disposal). Some related resources are this article and this question. Since you’re in ASP.Net MVC land make sure you also look into the MVC integration stuff.

    To illustrate the point, here’s a quick sample on how you can use Autofac factory delegates and the Owned generic to get full control over instance lifetime:

    public class SomeController
    {
        private readonly Func<Owned<ISession>> _sessionFactory;
    
        public SomeController(Func<Owned<ISession>> sessionFactory)
        {
            _sessionFactory = sessionFactory;
        }
    
        public void DoSomeWork()
        {
            using (var session = _sessionFactory())
            {
                 var transaction = session.Value.BeginTransaction();
                 ....   
            }
        }
    }
    

    The container setup to get this to work is quite simple. Notice that we don’t have to do anything to get the Func<> and Owned<> types, these are made available automatically by Autofac:

    builder.Register(c => cfg.BuildSessionFactory())
        .As<ISessionFactory>()
        .SingleInstance();
    builder.Register(c => c.Resolve<ISessionFactory>().OpenSession());
    

    Update: my reasoning here is that, according to this NHibernate tutorial, the lifetime of the session instance should be that of the “unit of work”. Thus we need some way of controlling both when the session instance is created and when the session is disposed.

    With Autofac we get this control by requesting a Func<> instead of the type directly. Not using Func<> would require that the session instance be created upfront before the controller instance is created.

    Next, the default in Autofac is that instances have the lifetime of their container. Since we know that we need the power to dispose this instance as soon as the unit of work is done, we request an Owned instance. Disposing the owned instance will in this case immediately dispose the underlying session.

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

Sidebar

Related Questions

Does anyone have any tips that could help speed up a process of breaking
Does anyone have any tips regarding making animated GIFs in R? I'm trying to
I'm trying to locate php_intl.dll and install it. Does anyone have any tips?
Does anyone have any idea how I can go about creating an infinite scroll.
My question has two parts: Does anyone have any tips or references to some
Does anyone have any tips on making a pure JS image slider? I am
Does anyone have any tips for dealing with ConstraintExceptions thrown by XSD datasets? This
Does anyone have any tips or gotcha moments to look out for when trying
Does anyone have any tips for debugging exceptions in a C# object initializer block?
Does anyone have any tips on working with Amazon S3 service and Flash? I

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.