这就是我做事的方式可能对你有用。我使用 Fluent Nhibernate 以防某些配置无法运行。
public interface INHibernateSessionFactoryHelper
{
ISessionFactory CreateSessionFactory();
}
public class NhibernateSessionFactoryHelper
{
private static readonly string ConnectionString =
ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.ProxyFactoryFactory("NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate")
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<EntityMap>())
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(ConnectionString).AdoNetBatchSize(1000))
.Cache(
c =>
c.ProviderClass<SysCacheProvider>().UseSecondLevelCache().UseQueryCache().UseMinimalPuts())
.ExposeConfiguration(c => c.SetProperty(Environment.GenerateStatistics, "true")
.SetProperty(Environment.SessionFactoryName, "My Session Factory")
.SetProperty(Environment.CurrentSessionContextClass, "web"))
.Diagnostics(d => d.Enable().OutputToFile(@"c:\temp\diags.txt"))
.BuildSessionFactory();
}
}
然后我的 Windsor 安装程序看起来像这样
public class NHibernateInstaller:IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<ISessionFactory>().Instance(NhibernateSessionFactoryHelper.CreateSessionFactory()));
container.Register(Component.For<ISessionManager>().ImplementedBy<SessionManager>().LifestylePerWebRequest());
}
}
我省略了我使用的 SessionManager 的代码。让我知道你是否愿意
UPDTAE:这是我用于管理会话和事务的代码(我发现这些代码分散在 Internet 上,但没有太多修改就可以很好地工作。ISessionManager 按照我之前的示例连接起来并注入到我的服务的构造函数中.
public interface ISessionManager : IDisposable
{
ISession Session { get; set; }
ISession GetSession();
}
public class SessionManager : ISessionManager
{
private readonly ISessionFactory _sessionFactory;
private TransactionScope _scope;
public SessionManager(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
#region ISessionManager Members
public ISession Session { get; set; }
public ISession GetSession()
{
if (Session == null)
{
Session = _sessionFactory.OpenSession();
if (!CurrentSessionContext.HasBind(_sessionFactory))
{
_scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted});
Session.BeginTransaction(IsolationLevel.ReadCommitted);
CurrentSessionContext.Bind(Session);
}
}
Session = _sessionFactory.GetCurrentSession();
Session.FlushMode = FlushMode.Never;
return Session;
}
public void Dispose()
{
if (CurrentSessionContext.HasBind(_sessionFactory))
{
CurrentSessionContext.Unbind(_sessionFactory);
}
try
{
Session.Transaction.Commit();
_scope.Complete();
_scope.Dispose();
Session.Flush();
}
catch (Exception)
{
if (Session.Transaction != null && Session.Transaction.IsActive)
{
Session.Transaction.Rollback();
}
throw;
}
finally
{
Session.Close();
Session.Dispose();
}
}
#endregion
}
示例构造函数:
private readonly ISessionManager _sessionManager;
private readonly ISession _session;
public UserService(ISessionManager sessionManager)
{
_sessionManager = sessionManager;
_session = sessionManager.GetSession();
}