view Agendas/trunk/src/Agendas.NHibernate/NHibernateSessionPerActionAttribute.cs @ 67:c8099df941bd

Implementando persistencia con NHibernate en el proyecto web
author nelopauselli
date Thu, 19 May 2011 01:24:56 -0300
parents
children
line wrap: on
line source

using System.Web.Mvc;
using NHibernate;
using NHibernate.Context;

namespace Agendas.NHibernate
{
	public class NHibernateSessionPerActionAttribute : ActionFilterAttribute
	{
		private readonly ISessionFactory _sessionFactory;

		public NHibernateSessionPerActionAttribute(ISessionFactory sessionFactory)
		{
			_sessionFactory = sessionFactory;
		}

		public override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			var session = _sessionFactory.OpenSession();
			CurrentSessionContext.Bind(session);

			base.OnActionExecuting(filterContext);
		}

		public override void OnResultExecuted(ResultExecutedContext filterContext)
		{
			base.OnResultExecuted(filterContext);

			var session = _sessionFactory.GetCurrentSession();

			session.Flush();
			session.Close();

		}
	}
}