comparison Agendas/trunk/src/Agendas.Repositories.NHibernate/NHibernateSessionPerRequestAttribute.cs @ 67:c8099df941bd

Implementando persistencia con NHibernate en el proyecto web
author nelopauselli
date Thu, 19 May 2011 01:24:56 -0300
parents
children
comparison
equal deleted inserted replaced
64:240a20cdbcc8 67:c8099df941bd
1 using System.Web.Mvc;
2 using NHibernate;
3 using NHibernate.Context;
4
5 namespace AltNetHispano.Agendas.Repositories.NHibernate
6 {
7 public class NHibernateSessionPerRequestAttribute: ActionFilterAttribute
8 {
9 private readonly ISessionFactory _sessionFactory;
10
11 public NHibernateSessionPerRequestAttribute(ISessionFactory sessionFactory)
12 {
13 _sessionFactory = sessionFactory;
14 }
15
16 public override void OnActionExecuting(ActionExecutingContext filterContext)
17 {
18 //iniciar session
19 var session = _sessionFactory.OpenSession();
20 CurrentSessionContext.Bind(session);
21
22 base.OnActionExecuting(filterContext);
23 }
24
25 public override void OnResultExecuted(ResultExecutedContext filterContext)
26 {
27 base.OnResultExecuted(filterContext);
28
29 //cerrar session
30 var session = _sessionFactory.GetCurrentSession();
31 session.Flush();
32 session.Close();
33
34 }
35 }
36 }