NHibernate and Structure Map

前端 未结 3 609
Happy的楠姐
Happy的楠姐 2020-12-07 23:59

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 Sp

3条回答
  •  余生分开走
    2020-12-08 00:25

    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().AsSingletons().TheDefault.IsThis(cfg);
    
                ForRequestedType().AsSingletons()
                    .TheDefault.IsThis(sessionFactory);
    
                ForRequestedType().CacheBy(InstanceScope.Hybrid)
                    .TheDefault.Is.ConstructedBy(ctx => ctx.GetInstance().OpenSession());
    
                ForRequestedType().CacheBy(InstanceScope.Hybrid)
                    .TheDefaultIsConcreteType();
    
                    ForRequestedType().TheDefaultIsConcreteType();
            }
        }
    }
    

    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();
    
            }
    
            private void ContextEndRequest(object sender, EventArgs e)
            {
                Dispose();
            }
    
            public void Dispose()
            {
                _unitOfWork.Dispose();
            }
        }
    }
    

提交回复
热议问题