所以我非常喜欢使用NHibernate,但总是使用Spring.Net.
我最近遇到了Jeremy Miller的StructureMap,并且比Spring.Net更喜欢它.在他的StructureMap网站上,他承诺一个关于如何一起使用NHibernate和StructureMap的例子.不幸的是,他没有时间去做(或者我找不到).
那么有没有人有关于如何使用StructureMap处理NHibernate会话的例子?
所以,我很抱歉我们没有使用之前完成的StructureMap示例获得NHibernate.最后,我想在StructureMap文档中发布它,但我首先需要一些反馈.你可以在我的博客上看到完整的例子:
http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html
话虽这么说,我可以点击这里的亮点.有一个NHibernateRegistry提供四个功能:NHibernate.Configuration(作为Singleton),一个ISessionFactory(作为Singleton),一个ISession(作用域混合(HttpContext,如果可用,回退到Thread本地存储)),以及一个非常好的简单的IUnitOfWork.此外,还有一个HttpModule来管理每个Web请求的UnitOfWork.
这是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 (); } } }
以下是工作单元的代码:
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(); } } }
以下是适用于Web应用程序的NHibernateModule:
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(); } } }