Guest User

NHibernateActionFilter

a guest
Aug 10th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using CumbriaMD.Web.Controllers;
  7. using NHibernate;
  8. using NHibernate.Cfg;
  9.  
  10. namespace CumbriaMD.Web.Filters
  11. {
  12.     public class NHibernateActionFilter : ActionFilterAttribute
  13.     {
  14.         private static readonly ISessionFactory sessionFactory = BuildSessionFactory();
  15.  
  16.         private static ISessionFactory BuildSessionFactory()
  17.         {
  18.             return new Configuration()
  19.                 .Configure()
  20.                 .BuildSessionFactory();
  21.         }
  22.  
  23.         public override void OnActionExecuting(ActionExecutingContext filterContext)
  24.         {
  25.             var sessionController = filterContext.Controller as SessionController;
  26.  
  27.             if (sessionController == null)
  28.                 return;
  29.  
  30.             sessionController.Session = sessionFactory.OpenSession();
  31.         }
  32.  
  33.         public override void OnActionExecuted(ActionExecutedContext filterContext)
  34.         {
  35.             var sessionController = filterContext.Controller as SessionController;
  36.  
  37.             if (sessionController == null)
  38.                 return;
  39.  
  40.             var session = sessionController.Session;
  41.             if (session == null)
  42.                 return;
  43.  
  44.             session.Dispose();
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment