Skip to content

Commit

Permalink
Moving all nhibernate session management logic to a NHibernateActionF…
Browse files Browse the repository at this point in the history
…ilter
  • Loading branch information
ayende committed Mar 29, 2011
1 parent a7677fd commit 02c76d4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
8 changes: 3 additions & 5 deletions CourseSampleApp/Controllers/SessionController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Web;
using System.Web.Mvc;
using CourseSampleApp.Infrastructure;
using NHibernate;

namespace CourseSampleApp.Controllers
Expand All @@ -8,12 +9,9 @@ public class SessionController : Controller
{
public HttpSessionStateBase HttpSession
{
get { return base.Session; }
get { return base.Session; }
}

public new ISession Session
{
get { return MvcApplication.CurrentSession; }
}
public new ISession Session { get { return NHibernateActionFilter.CurrentSession; } }
}
}
1 change: 1 addition & 0 deletions CourseSampleApp/CourseSampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Infrastructure\NHibernateActionFilter.cs" />
<Compile Include="Models\Blog.cs" />
<Compile Include="Models\User.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
33 changes: 2 additions & 31 deletions CourseSampleApp/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using CourseSampleApp.Infrastructure;
using NHibernate;
using NHibernate.Cfg;

Expand All @@ -14,33 +15,10 @@ namespace CourseSampleApp

public class MvcApplication : System.Web.HttpApplication
{
private static readonly ISessionFactory sessionFactory = BuildSessionFactory();

public static ISession CurrentSession
{
get{ return HttpContext.Current.Items["NHibernateSession"] as ISession;}
set { HttpContext.Current.Items["NHibernateSession"] = value; }
}

public MvcApplication()
{
BeginRequest += (sender, args) =>
{
CurrentSession = sessionFactory.OpenSession();
};
EndRequest += (o, eventArgs) =>
{
var session = CurrentSession;
if (session != null)
{
session.Dispose();
}
};
}

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new NHibernateActionFilter());
}

public static void RegisterRoutes(RouteCollection routes)
Expand All @@ -62,12 +40,5 @@ protected void Application_Start()
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}

private static ISessionFactory BuildSessionFactory()
{
return new Configuration()
.Configure()
.BuildSessionFactory();
}
}
}
41 changes: 41 additions & 0 deletions CourseSampleApp/Infrastructure/NHibernateActionFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Web;
using System.Web.Mvc;
using CourseSampleApp.Controllers;
using NHibernate;
using NHibernate.Cfg;

namespace CourseSampleApp.Infrastructure
{
public class NHibernateActionFilter : ActionFilterAttribute
{
private static readonly ISessionFactory sessionFactory = BuildSessionFactory();

public static ISession CurrentSession
{
get { return HttpContext.Current.Items["NHibernateSession"] as ISession; }
set { HttpContext.Current.Items["NHibernateSession"] = value; }
}

private static ISessionFactory BuildSessionFactory()
{
return new Configuration()
.Configure()
.BuildSessionFactory();
}

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
CurrentSession = sessionFactory.OpenSession();
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{

var session = CurrentSession;
if (session != null)
{
session.Dispose();
}
}
}
}

0 comments on commit 02c76d4

Please sign in to comment.