在.net实现AOP

本文通过一个简单的例子实现静态AOP.改例子主要实现客户的增删改查,突然有一天你的老板需要在程序上跟踪每个方法操作的运行日志。

主要分为5个步骤。

第一步:创建接口IRepository<T>,代码定义如下:

    public interface IRepository<T>
    {
      void Add(T entity);
      void Delete(T entity);
      void Update(T entity);
      IEnumerable<T> GetAll();
      T GetById(int id);
    }

第2步: 创建Repository<T>类,实现代码IRepository<T>,代码如下:

    public class Repository<T> : IRepository<T>
    {
      public void Add(T entity)
      {
    Console.WriteLine("Adding {0}", entity);
      }
      public void Delete(T entity)
      {
    Console.WriteLine("Deleting {0}", entity);
      }
      public void Update(T entity)
      {
    Console.WriteLine("Updating {0}", entity);
      }
      public IEnumerable<T> GetAll()
      {
    Console.WriteLine("Getting entities");
    return null;
      }
      public T GetById(int id)
      {
    Console.WriteLine("Getting entity {0}", id);
    return default(T);
      }
    }

第3步,用Repository<T>实现Customer 的增删改查,Customer 定义如下:

    public class Customer
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Address { get; set; }
    }

第4步,在控制台程序,运行,代码看起来如下:

    static void Main(string[] args)
    {
      Console.WriteLine("***\r\n Begin program - no logging\r\n");
      IRepository<Customer> customerRepository =new Repository<Customer>();
      var customer = new Customer
      {
        Id = 1,
        Name = "Customer 1",
        Address = "Address 1"
      };
      customerRepository.Add(customer);
      customerRepository.Update(customer);
      customerRepository.Delete(customer);
      Console.WriteLine("\r\nEnd program - no logging\r\n***");
      Console.ReadLine();
    }

按F5运行,运行结果如下图:

现在假如你的老板,需要在类中增加日志功能,你可以新建一个LoggerRepository<T>类,让他同样实现IRepository<T>,代码如下:

    public class LoggerRepository<T> : IRepository<T>
    {
      private readonly IRepository<T> _decorated;
      public LoggerRepository(IRepository<T> decorated)
      {
            _decorated = decorated;
      }
      private void Log(string msg, object arg = null)
      {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(msg, arg);
        Console.ResetColor();
       }
      public void Add(T entity)
      {
        Log("In decorator - Before Adding {0}", entity);
        _decorated.Add(entity);
        Log("In decorator - After Adding {0}", entity);
      }
      public void Delete(T entity)
      {
        Log("In decorator - Before Deleting {0}", entity);
        _decorated.Delete(entity);
        Log("In decorator - After Deleting {0}", entity);
      }
      public void Update(T entity)
      {
        Log("In decorator - Before Updating {0}", entity);
        _decorated.Update(entity);
        Log("In decorator - After Updating {0}", entity);
      }
      public IEnumerable<T> GetAll()
      {
        Log("In decorator - Before Getting Entities");
        var result = _decorated.GetAll();
        Log("In decorator - After Getting Entities");
        return result;
      }
      public T GetById(int id)
      {
        Log("In decorator - Before Getting Entity {0}", id);
        var result = _decorated.GetById(id);
        Log("In decorator - After Getting Entity {0}", id);
        return result;
      }
    }

 

这个新类,已经包装了包装类,实现日志记录功能,现在需要做小的改动,代码实现如下:

 

static void Main(string[] args)
    {
      Console.WriteLine("***\r\n Begin program - logging with decorator\r\n");
      // IRepository<Customer> customerRepository =
      //   new Repository<Customer>();
      IRepository<Customer> customerRepository =
    new LoggerRepository<Customer>(new Repository<Customer>());
      var customer = new Customer
      {
        Id = 1,
        Name = "Customer 1",
        Address = "Address 1"
      };
      customerRepository.Add(customer);
      customerRepository.Update(customer);
      customerRepository.Delete(customer);
      Console.WriteLine("\r\nEnd program - logging with decorator\r\n***");
      Console.ReadLine();
    }

 

继续运行,运行结果如下:

 

到此为止,你可能会想:OK,这个想法是好的,但它仍然需要做很多工作:我必须实现所有的类和添加方面的所有方法。这将是难以维护。

有另一种方式去做吗?.net框架,您可以使用反射来得到所有方法和执行它们。基类库(BCL)甚至有RealProxy类(bit.ly/18MfxWo),为你实现。

答案:是的有的就是我们所讲的动态代理实现AOP; 我们将在下一节讲

AOP参考

本文翻译自 :https://msdn.microsoft.com/en-us/magazine/dn574804.aspx(面向切面编程)

静态实现AOP(翻译自MSDN)的更多相关文章

  1. AOP(面向切面编程,翻译自MSDN)

    目录 AOP的概念 静态实现AOP .Net 框架实现AOP(动态代理实现AOP) 动态代理AOP实现方法过滤 AOP参考 本文翻译自 :https://msdn.microsoft.com/en-u ...

  2. Creating Icon Overlay Handlers / 创建图标标记 Handlers (翻译自MSDN) / VC++, Windows, DLL, ATL, COM

    创建图标标记 Handlers Creating Icon Overlay Handlers 图标标记是放在代表着某个 Shell 对象的图标之左下角的小图像.它们通常被加在一个对象的图标的身上来提供 ...

  3. 【原创翻译】链接DLL至可执行文件---翻译自MSDN

    可执行文件.exe链接(或加载)DLL有以下两种形式: 隐式链接 显式链接 隐式链接是指静态加载或在程序加载时动态链接. 通过隐式链接,在使用DLL时,可执行文件链接到一个由生成DLL的人提供的导入函 ...

  4. HTML(Open Method)翻译自MSDN

    Open Method Opens a new window and loads the document specified by a given URL. Navigates the app wi ...

  5. .Net 框架实现AOP(动态代理实现AOP,本文为翻译)

    在上一节,我们将静态实现AOP,但是对于一个大型项目,要想为每个类,每个方法都去实现AOP ,进行日志记录和权限验证似乎是不可能的. 即使可能对于成百上千个类维护,也是很难维护.所以今天的主题就是如标 ...

  6. Spring AOP——Spring 中面向切面编程

    前面两篇文章记录了 Spring IOC 的相关知识,本文记录 Spring 中的另一特性 AOP 相关知识. 部分参考资料: <Spring实战(第4版)> <轻量级 JavaEE ...

  7. 漫谈AOP开发之初探AOP及AspectJ的用法

    一.为什么需要AOP技术 AOP 是一个很成熟的技术. 假如项目中有方法A.方法B.方法C……等多个方法, 如果项目需要为方法A.方法B.方法C……这批方法增加具有通用性质的横切处理.   下图可以形 ...

  8. Attribute(特性)与AOP

    提到特性,好多人都会疑惑特性(attribute),和注释有什么区别,简单来说,特性是给机器看的,而注释是给人看的. 特性不仅可以影响编译还可以影响运行,而注释只是为了让人更加容易理解.看懂代码而特别 ...

  9. angular2 学习笔记 ( translate, i18n 翻译 )

    更新 : 2017-06-17 <h1 i18n="site header|An introduction header for this sample">Hello ...

随机推荐

  1. git下载别人的代码

    1. 打开别人github上的源码地址,点击Clone or download 2. 拷贝链接 3. 通过git clone URL来下载 此外,还可以通过pwd来查看当前目录的路径,一般都是下载到当 ...

  2. 在阿里云上安装python3.4和pycharm

    一. 安装python3.4 二. 安装pycharm 三. 安装可视化界面和远程桌面连接 四. 启动和配置pycharm 五. 安装更多字体 六. 给pycharm设置桌面快捷方式 一. 安装pyt ...

  3. Workflow Builder 2.6.3 Certified on Windows 10 for EBS 12.x

    By Steven Chan - EBS-Oracle on May 17, 2016 Workflow Builder 2.6.3 is now certified on Windows 10 de ...

  4. js & jquery数组介绍

    (转自:http://www.jb51.net/article/30793.htm) 1.数组的创建 var arr=new Array(); 2.查找数组中的元素 for(var i=0;i< ...

  5. LoadRunner压力测试实际运用的使用方法

    LoadRunner 是一种预测系统行为和性能的工业标准级负载测试工具.通过以模拟上 千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner 能够对整个 企业架构进行测试. 方 ...

  6. react 学习文章

    生命周期 学习笔记 一些坑 项目完成后总结 理解Immutable 是否要同构如何同构 react组件最佳实践 redux集合所有的state props来源, 页面所有状态 数据的唯一来源 reac ...

  7. Django 常见的异常

    Django 常见的异常 1 'WSGIRequest' object has no attribute 'user' Django版本的问题,1.10之前,中间件的key为MIDDLEWARE_CL ...

  8. 打包发布自己的nodejs包

    下午的时候写了一篇关于一个不成熟的模板引擎的博客,觉得还是不太够,然后就封装了起来,做成了一款开发包.最后为了尝试一下如何发布自己的包,就又完善了一下.做此文以记之. 初衷 说来也不是什么高大上的东西 ...

  9. keras中自定义Layer

    最近在学习SSD的源码,其中有两个自定的层,特此学习一下并记录. import keras.backend as K from keras.engine.topology import InputSp ...

  10. 机器人研发十大热门编程语言:不死 Java、不朽 C/C ++、新贵 Python

    流水的编程语言,铁打的 Java.C/C++. 进行人工智能机器人研发,应该选择哪种编程语言? 这是很多机器人专家在自身的职业生涯中都会存在的一个入门级思考.毕竟,在学习一门编程语言时,需要花费大量的 ...