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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:17:03+00:00 2026-05-12T17:17:03+00:00

So I really like working with NHibernate but always used Spring.Net with it. I

  • 0

So I really like working with NHibernate but always used Spring.Net with it.

I recently came across StructureMap by Jeremy Miller and really like it better than Spring.Net. On his StructureMap site he promises an example on how to use NHibernate and StructureMap together. Unfortunately he has not had time to do it (or I can’t find it).

So does anyone have an example on how to handle the NHibernate Session with StructureMap?

  • 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-12T17:17:03+00:00Added an answer on May 12, 2026 at 5:17 pm

    So, I apologize that we did not get the NHibernate with StructureMap example done earlier. Eventually, I would like to publish it in the StructureMap documentation, but I need some feedback first. You can see the full example on my blog:

    http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html

    That being said, I can hit the highlights here. There is an NHibernateRegistry that makes available four things: an NHibernate.Configuration (as a Singleton), an ISessionFactory (as a Singleton), an ISession (scoped Hybrid (HttpContext if available, falling back to Thread local storage)), and a very simple IUnitOfWork. Also, there is an HttpModule to manage the UnitOfWork per web request.

    Here is the code for the NHibernateRegistry:

    using NHibernate;
    using NHibernate.ByteCode.Castle;
    using NHibernate.Cfg;
    using NHibernate.Dialect;
    using NHibernate.Driver;
    using NHibernateBootstrap.Core.Domain;
    using StructureMap.Attributes;
    using StructureMap.Configuration.DSL;
    using Environment=NHibernate.Cfg.Environment;
    
    namespace NHibernateBootstrap.Core.Persistence
    {
        public class NHibernateRegistry : Registry
        {
            public NHibernateRegistry()
            {
                var cfg = new Configuration()
                    .SetProperty(Environment.ReleaseConnections, "on_close")
                    .SetProperty(Environment.Dialect, typeof(SQLiteDialect).AssemblyQualifiedName)
                    .SetProperty(Environment.ConnectionDriver, typeof(SQLite20Driver).AssemblyQualifiedName)
                    .SetProperty(Environment.ConnectionString, "data source=bootstrap.sqlite;Version=3")
                    .SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
                    .AddAssembly(typeof(Blog).Assembly);
    
                var sessionFactory = cfg.BuildSessionFactory();
    
                ForRequestedType<Configuration>().AsSingletons().TheDefault.IsThis(cfg);
    
                ForRequestedType<ISessionFactory>().AsSingletons()
                    .TheDefault.IsThis(sessionFactory);
    
                ForRequestedType<ISession>().CacheBy(InstanceScope.Hybrid)
                    .TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance<ISessionFactory>().OpenSession());
    
                ForRequestedType<IUnitOfWork>().CacheBy(InstanceScope.Hybrid)
                    .TheDefaultIsConcreteType<UnitOfWork>();
    
                    ForRequestedType<IDatabaseBuilder>().TheDefaultIsConcreteType<DatabaseBuilder>();
            }
        }
    }
    

    Here is the code for the Unit of Work:

    using System;
    using NHibernate;
    
    namespace NHibernateBootstrap.Core.Persistence
    {
        public interface IUnitOfWork : IDisposable
        {
            ISession CurrentSession { get; }
            void Commit();  
        }
    }
    
    using NHibernate;
    
    namespace NHibernateBootstrap.Core.Persistence
    {
        public class UnitOfWork : IUnitOfWork
        {
            private readonly ISessionFactory _sessionFactory;
            private readonly ITransaction _transaction;
    
            public UnitOfWork(ISessionFactory sessionFactory)
            {
                _sessionFactory = sessionFactory;
                CurrentSession = _sessionFactory.OpenSession();
                _transaction = CurrentSession.BeginTransaction();
            }
    
            public ISession CurrentSession { get; private set;}
    
            public void Dispose()
            {
                CurrentSession.Close();
                CurrentSession = null;
            }
    
            public void Commit()
            {
                _transaction.Commit();
            }
        }
    }
    

    Here is the NHibernateModule for web applications:

    using System;
    using System.Web;
    using NHibernateBootstrap.Core.Persistence;
    using StructureMap;
    
    namespace NHibernateBootstrap.Web
    {
        public class NHibernateModule : IHttpModule
        {
            private IUnitOfWork _unitOfWork;
    
            public void Init(HttpApplication context)
            {
                context.BeginRequest += ContextBeginRequest;
                context.EndRequest += ContextEndRequest;
            }
    
            private void ContextBeginRequest(object sender, EventArgs e)
            {
                _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>();
    
            }
    
            private void ContextEndRequest(object sender, EventArgs e)
            {
                Dispose();
            }
    
            public void Dispose()
            {
                _unitOfWork.Dispose();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already have a working solution, but I would really like to know why
This probably sounds like a nightmare, but I'd really like to get this working.
I have started using MVC 3 and I really like working with it. It's
I'm working on a REST API and I really like the idea of using
I really need some help in Regular Expressions, i'm working on a function like
I really like the magento structure but finding things is very hard ;) My
I really like the MVC way and have actually enjoyed learning ASP.NET MVC (I
I really like the promote to home page feature of drupal. But what if
Right now I have some functionality working but its really ugly and I know
I tried working with ProtoRPC and I really like how easily I can add

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.