ASP.NET MVC 与 NHibernate结合使用时,NHibernate的session问题
代码是这样的
[NHibernateSession]
public ActionResult SaveNewOrder(Order order)
{
......
return RedirectToAction("Index");
}
[AttributeUsageAttribute(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class NHibernateSessionAttribute : FilterAttribute, IActionFilter
{
public NHibernateSessionAttribute() : base()
{
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
var session = SessionProvider.GetNewSession();
CurrentSessionContext.Bind(session);
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
CurrentSessionContext.Unbind(SessionProvider.GetSessionFactory()).Close();
}
}