zoukankan      html  css  js  c++  java
  • MVC中使用 事物

    1,MVC中所有的控制器都 继承自 Controller  

    2,我们要对Controller进行处理  就要写一个类来继承自Controller    重写方法   加入自己的处理程序

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using CDS.CustomAttribute;
    using System.Transactions;
    
    namespace CDS.Manager
    {
        [HandleError]
        public class CDSController : Controller
        {
            #region action发生异常
            /// <summary>
            /// action发生异常
            /// </summary>
            /// <param name="filterContext"></param>
            protected override void OnException(ExceptionContext filterContext)
            {
                Session["actionException"] = "";
                base.OnException(filterContext);
            }
            #endregion
    
            #region 执行action
            /// <summary>
            /// 执行action
            /// </summary>
            protected override void ExecuteCore()
            {
                if (IsTransationAction())//当前action使用事务
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        Session["actionException"] = null;
                        base.ExecuteCore();
    
                        if (Session["actionException"] == null)//没有发生异常才提交数据库
                        {
                            scope.Complete();
                        }
                        Session["actionException"] = null;
                    }
                }//end action使用事务
                else
                {
                    base.ExecuteCore();
                }
            }
            #endregion
    
            #region 判断当前请求的action是否使用事务
            /// <summary>
            /// 判断当前请求的action是否使用事务
            /// </summary>
            /// <returns></returns>
            public bool IsTransationAction()
            {
                bool bTrans = false;
    
                try
                {
                    ControllerContext controllerContext = new ControllerContext(new System.Web.Routing.RequestContext(HttpContext, RouteData), this);
    
                    string actionName = RouteData.GetRequiredString("action");
                    Type tControl = controllerContext.Controller.GetType();
    
                    ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(tControl);
                    ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext, actionName);
    
    
                    object[] attrTrans = actionDescriptor.GetCustomAttributes(typeof(IsTransationAction), false);
    
                    if (attrTrans != null && attrTrans.Length > 0 && ((CDS.CustomAttribute.IsTransationAction)(attrTrans[0])).IsTransaction)
                    {
                        bTrans = true;
                    }
                }
                catch
                {
    
                }
    
                return bTrans;
            }
    
    
            #endregion
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace CDS.CustomAttribute
    {
        [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
        public class IsTransationAction : Attribute
        {
            private bool b_isTransaction = false;
    
            public IsTransationAction(bool isTransaction)
            {
                this.b_isTransaction = isTransaction;
            }
    
            public bool IsTransaction
            {
                get
                {
                    return (this.b_isTransaction);
                }
                set
                {
                    this.b_isTransaction = value;
                }
            }
        }
    }
  • 相关阅读:
    cocos2d-x Mask的实现及优化
    cocos2d-x CCTableView
    cocos2d-x CCScrollView
    网络服务端开发
    跨平台web调试代理工具---whistle
    探索c#之一致性Hash详解
    Redis性能问题排查解决手册
    Redis的Aof被阻塞原因调查
    AOF 持久化策略
    如何招到一个靠谱的程序员
  • 原文地址:https://www.cnblogs.com/DamonTang/p/2453395.html
Copyright © 2011-2022 走看看