zoukankan      html  css  js  c++  java
  • 利用反射+AOP,封装Basehandler

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式运行期动态代理实现程序功能统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。AOP实际是GoF设计模式的延续设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,提高代码的灵活性和可扩展性,AOP可以说也是这种目标的一种实现。例如,在Spring中提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。

    总结,所有设计模式追求的是调用者与被调用者的解耦(这样可以提高代码的灵活性和可扩展性),AOP作为一种设计模式的延伸,它分离了应用的业务逻辑与系统级服务(不仔细分都是业务逻辑),通过预编译和动态代理实现程序功能的。(想一想shtml中的#include,[Obsolete("hehe", false)]不也是一种标记,然后动态执行吗,另外,预处理和动态执行应该总是结合在一起使用的



    public class test1 : IHttpHandler
        {
            [ObsoleteAttribute("hehe", false)]
            public void ProcessRequest(HttpContext context)
            {
               
                Type type = typeof(test1);
                object[] attr = type.GetMethod("ProcessRequest").GetCustomAttributes(typeof(ObsoleteAttribute), false);
                ObsoleteAttribute ra= (ObsoleteAttribute)attr[0];
                string ss = ra.Message;  //hehe    
                bool ii = ra.IsError;    //false
    
            }
    Attribute可以省略,附加的Attribute类中的属性只能是常量。([ObsoleteAttribute(DateTime.Now.ToString(), false)],是错误的)。
    
    
     自定义Attribute
       public class LsAttribute:Attribute
        {
            
            private string message;
    
            public string Message
            {
                get { return this.message; }
                set { this.message = value; }
            }
            public LsAttribute(string message) {
                Message = message;
            }
            public LsAttribute() { }
    
        }
    }

    完整调用

            [Obsolete("hehe", false)]
            //[LsAttribute(Message = "first")]   第一种方式
            [LsAttribute("second")]    //第二种方式
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "application/json";
                string action = context.Request["action"];
                if (action == "test")
                {
                    string checkUserName = context.Request["checkUserName"];
                    string username = context.Request["username"];
                    string CheckPwd = context.Request["CheckPwd"];
                    string password = context.Request["password"];
                    if (checkUserName == "on" && CheckPwd == "on")
                    {
                        AjaxHelper.WriteJson(context.Response, "ok", "", new { username = username, password = password });
                    }
                }
    
                Type type = typeof(test1);
    
                object[] attr1 = type.GetMethod("ProcessRequest").GetCustomAttributes(typeof(ObsoleteAttribute), false);
                ObsoleteAttribute ra = (ObsoleteAttribute)attr1[0];
                string ss = ra.Message;
                bool ii = ra.IsError;
    
                object[] attr2 = type.GetMethod("ProcessRequest").GetCustomAttributes(typeof(LsAttribute), false);
                LsAttribute ls=(LsAttribute)attr2[0];
                string lsMessage = ls.Message;
    
            }     

     最后应用到实际BaseHandler

     public class BaseHandler:IHttpHandler,IRequiresSessionState
        {
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
            
    
            public void ProcessRequest(HttpContext context)
            {
                AdminHelper.CheckAccess(context);
                string action=context.Request["action"];
                //规定:参数总都有一个action 参数,表示执行什么方法
                //action要和处理这个action的方法名一样,并且有如下参数(HttpContext context)
                //例如 action=list,则有一个方法 public void list(HttpContext context)
                Type ctrlType = this.GetType();//得到子类类型,例如,CourseController
                MethodInfo methodAction = ctrlType.GetMethod(action);//例如,拿到子类的list方法
                if (methodAction==null)
                {
                    throw new Exception("action 不存在");
                }
                object[] paAttrs = methodAction.GetCustomAttributes(typeof(PowerActionAttribute), false);
                if (paAttrs.Length>0)
                {
                    PowerActionAttribute powerAction=(PowerActionAttribute)paAttrs[0];
                    AdminHelper.CheckPower(powerAction.Name);
                }
                methodAction.Invoke(this, new object[] { context });
            }
        }
    PowerActionAttribute
     [AttributeUsage(AttributeTargets.Method)]
        public class PowerActionAttribute:Attribute
        {
            public string Name { get; set; }
            public PowerActionAttribute(string name)
            {
                this.Name = name;
            }
        }


  • 相关阅读:
    android webview 使用以及一些异常处理及上传文件问题
    bootstrap 下拉菜单不显示的解决办法
    无损图片缩放
    startssl,免费的ssl证书申请及注意事项
    iis7.5
    vs2013 类名颜色显示黑色,无法修改
    "未能找到类型或命名空间名称",引用dll的时候出错
    由于 add 运算符中“Chinese_PRC_CI_AS”和“Chinese_PRC_CS_AS_WS”之间的排序规则冲突
    asp.net mvc int[] 和 string[] 自定义数组绑定
    windbg定位WEB性能瓶颈案例一则
  • 原文地址:https://www.cnblogs.com/John-Marnoon/p/5951616.html
Copyright © 2011-2022 走看看