开发者

Transaction Filter Exception handling ASP.NET MVC

开发者 https://www.devze.com 2022-12-19 15:07 出处:网络
I am using S#arp Architecture on a project, which comes with the [Transaction] attribute for Controller methods.With this, the Transaction Commit is called as a OnActionExecuted filter, meaning it occ

I am using S#arp Architecture on a project, which comes with the [Transaction] attribute for Controller methods. With this, the Transaction Commit is called as a OnActionExecuted filter, meaning it occurs after exiting the Controller method scope. My issue with this is what happens when an exception occurs during the commit?

From the S#arp source code, you can see the following code in TransactionAttribute.cs

public override void OnActionExecuted(ActionExecutedContext filterContext) {
        string effectiveFactoryKey = GetEffectiveFactoryKey();

        ITransaction currentTransaction = 
            NHibernateSession.CurrentFor(effectiveFactoryKey).Transaction;

        if (currentTransaction.IsActive) {
            if (filterContext.Exception == null) {
                currentTransaction.Commit();
            }
            else {
                currentTransaction.Rollback();
            }
        }
    }

As an example, if the user attempted开发者_运维百科 to commit a save where there was a Foreign Key constraint (and had wrong data), the Commit will produce a Database exception that is not handled. Instead of dumping the user to a generic Error page (a la the [HandleError] construct), I'd rather return them right to the spot where they were, so they could correct the issue. I could do this if I do the transaction explicitly within the scope of the Controller method. I can't since as a post-filter, it is out of scope.

I'd like to see what others would do in this situation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号