我让Ninject使用以下注册方法管理我的ISession
和ITransaction
状态,使用以下注册方法 - 我想知道是否有足够的交易控制权,或者我是否需要投入这个别的地方。让Ninject管理我的交易状态,练习关注
思想是每个ISession
都是在请求上创建的,并且Ninject处理在该请求期间完成的所有事务的提交。
public class SessionModule : Ninject.Modules.NinjectModule
{
private static ISessionFactory sessionFactory;
public override void Load()
{
Bind<ISessionFactory>()
.ToMethod(c => CreateSessionFactory())
.InSingletonScope();
Bind<ISession>()
.ToMethod(c => OpenSession())
.InRequestScope()
.OnActivation(session =>
{
session.BeginTransaction();
session.FlushMode = FlushMode.Commit;
})
.OnDeactivation(session =>
{
if (session.Transaction.IsActive)
{
try
{
session.Flush();
session.Transaction.Commit();
}
catch
{
session.Transaction.Rollback();
}
}
});
}
/// <summary>
/// Create a new <see cref="NHibernate.ISessionFactory"/> to connect to a database.
/// </summary>
/// <returns>
/// A constructed and mapped <see cref="NHibernate.ISessionFactory"/>.
/// </returns>
private static ISessionFactory CreateSessionFactory()
{
if (sessionFactory == null)
sessionFactory = Persistence.SessionFactory.Map
(System.Web.Configuration
.WebConfigurationManager
.ConnectionStrings["Local"]
.ConnectionString
);
return sessionFactory;
}
/// <summary>
/// Open a new <see cref="NHibernate.ISession"/> from a <see cref="NHibernate.ISessionFactory"/>.
/// </summary>
/// <returns>
/// A new <see cref="NHibernate.ISession"/>.
/// </returns>
private static ISession OpenSession()
{
// check to see if we even have a session factory to get a session from
if (sessionFactory == null)
CreateSessionFactory();
// open a new session from the factory if there is no current one
return sessionFactory.OpenSession();
}
}
我已经研究使用System.Diagnostics.Debug.WriteLine
发生的事情的时候写的运行时,它确实这个样子的是做什么的我想它做的事。我问你,社区,这是否是好实践或不。这是我的理解。
在http://ayende.com/blog/default.aspx上无数个小时的阅读导致我重新评估了很多进行会话管理的方式。
大量挖掘nHibernate文档告诉我,我需要使用ITransaction
每次发生任何事情与我的数据库。
将管理置于属性中被认为是一个缺陷,因为它不符合上述声明。
每个单独的操作做ITransaction
是不正确的过程,因为它需要是(A)我的控制器能够访问的ISession
或(B)我IRepository<T>
有ITransaction
逻辑,这是我在以前被告知问题不是一个好的做法。
把我ITransaction
管理在HttpModule
增加非neccessary开销,因为它给我的ISession
的HttpContext的知识,这意味着我必须做某种注射入HttpRequest
(我可以用[Inject]
做,但它似乎并不明智)
这使我得出了这个结论。
- 交易应该在请求
ISession
时开始。这种情况发生在单个请求 - 一切都是由一个
ISession
- 封装当
ITransaction
完成,它需要致力于使二级高速缓存可以得到它的结果。
任何人都可以阐明这一点吗?我终于走上正轨吗?还是我完全错过了这个观点?
我在想,如果有挂接到'InRequestScope'开始/提交事务...感谢您的文章的方式! – user1068352 2012-12-10 14:45:14