【发布时间】:2016-02-07 13:01:08
【问题描述】:
是否有可能确保 VS 项目中使用特定方法(返回 Nhibernate 会话)的每个开发人员都将强制使用 using 模式。 例如:
public static ISession GetSession()
{
ISessionFactory holder = ActiveRecordMediator.GetSessionFactoryHolder();
ISessionScope activeScope = holder.ThreadScopeInfo.GetRegisteredScope();
ISession session = null;
var key = holder.GetSessionFactory(typeof(ActiveRecordBase));
if (activeScope == null)
{
session = holder.CreateSession(typeof(ActiveRecordBase));
}
return session;
}
我想确保每个调用这个方法的开发者都用 using 语句包围它,否则它会被 Visual Studio 视为错误并且不会编译。 (或者也许通过一些分析托管代码的工具)。
只能按以下方式使用:
using (var session = Nh.GetSession())
{
//use the session
}
【问题讨论】:
标签: c# memory-management nhibernate dispose idisposable