<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_BEAUTY OF CODE</title><subtitle type="text">Kenneth Byron</subtitle><id>http://feed.cnblogs.com/blog/u/16373/rss</id><updated>2011-05-13T18:12:00Z</updated><author><name>Kenneth Byron</name><uri>http://www.cnblogs.com/KENNETHBYRON/</uri></author><generator>CNBlogs BlogServer</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/KENNETHBYRON/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/16373/rss"/><entry><id>http://www.cnblogs.com/KENNETHBYRON/archive/2011/05/14/aspnetmvc_nhibernate_attribute_transaction.html</id><title type="text">asp.net mvc3 nhibernate 声明式事务</title><summary type="text">日常开发中，经常使用NHibernate事务。因为NHibernate不支持嵌套式事务操作，所以ASP.NET MVC中通常使用一个事务来封装一次请求中的数据库操作。下面是我认为比较漂亮的写法。using System.Web.Mvc;using NHibernate;namespace WebMVC.Filters{ public class Transactional : ActionFilterAttribute { protected ISession _Session; public Transactional() { _Session = DependencyResolver.Cu</summary><published>2011-05-13T18:12:00Z</published><updated>2011-05-13T18:12:00Z</updated><author><name>Kenneth Byron</name><uri>http://www.cnblogs.com/KENNETHBYRON/</uri></author><link rel="alternate" href="http://www.cnblogs.com/KENNETHBYRON/archive/2011/05/14/aspnetmvc_nhibernate_attribute_transaction.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/KENNETHBYRON/archive/2011/05/14/aspnetmvc_nhibernate_attribute_transaction.html"/><content type="html">&lt;p&gt;日常开发中，经常使用NHibernate事务。&lt;/p&gt;&#xD;
&lt;p&gt;因为NHibernate不支持嵌套式事务操作，所以ASP.NET MVC中通常使用一个事务来封装一次请求中的数据库操作。&lt;/p&gt;&#xD;
&lt;p&gt;下面是我认为比较漂亮的写法。&lt;/p&gt;&#xD;
&lt;pre &gt;using System.Web.Mvc;&#xD;
using NHibernate;&#xD;
&#xD;
namespace WebMVC.Filters&#xD;
{&#xD;
    public class Transactional : ActionFilterAttribute&#xD;
    {&#xD;
        protected ISession _Session;&#xD;
&#xD;
        public Transactional()&#xD;
        {&#xD;
            _Session = DependencyResolver.Current.GetService&amp;lt;ISession&amp;gt;();&#xD;
        }&#xD;
&#xD;
        public override void OnActionExecuting(ActionExecutingContext filterContext)&#xD;
        {&#xD;
            _Session.BeginTransaction();&#xD;
        }&#xD;
&#xD;
        public override void OnActionExecuted(ActionExecutedContext filterContext)&#xD;
        {&#xD;
            if (_Session == null || !_Session.Transaction.IsActive)&#xD;
                return;&#xD;
&#xD;
            if (filterContext.Exception != null)&#xD;
                _Session.Transaction.Rollback();&#xD;
            else&#xD;
                _Session.Transaction.Commit();&#xD;
        }&#xD;
    }&#xD;
}&lt;/pre&gt;&#xD;
&lt;p&gt;使用起来很方便。&lt;/p&gt;&#xD;
&lt;pre &gt;using System.Web.Mvc;&#xD;
using WebMVC.Filters;&#xD;
&#xD;
namespace WebMVC.Controllers&#xD;
{&#xD;
    public class CustomersController : Controller&#xD;
    {&#xD;
        protected ICustomersRepository _CustomersRepository;&#xD;
&#xD;
        public CustomersController(ICustomersRepository customersRepository)&#xD;
        {&#xD;
            _CustomersRepository = customersRepository;&#xD;
        }&#xD;
&#xD;
        [Transactional]&#xD;
        public ActionResult List()&#xD;
        {&#xD;
            return View(_CustomersRepository.FindAll());&#xD;
        }&#xD;
    }&#xD;
}&lt;/pre&gt;&lt;img src="http://www.cnblogs.com/KENNETHBYRON/aggbug/2046015.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/KENNETHBYRON/archive/2011/05/14/aspnetmvc_nhibernate_attribute_transaction.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
