转自:https://www.cnblogs.com/neverc/p/5241466.html

AOP介绍

面向切面编程(Aspect Oriented Programming,英文缩写为AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

AOP是OOP的延续,是软件开发中的一个热点.

常用于:

Authentication

Caching

Lazy loading

Transactions

AOP基本原理

普通类

1
2
3
4
5
6
7
8
9
class Person : MarshalByRefObject
{
    public string Say()
    {
        const string str = "Person's say is called";
        Console.WriteLine(str);
        return str;
    }
}

代理类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Proxy<T> : RealProxy where T : new()
{
    private object _obj;
    public Proxy(object obj)
        base(typeof(T))
    {
        _obj = obj;
    }
    public override IMessage Invoke(IMessage msg)
    {
        Console.WriteLine("{0}:Invoke前", DateTime.Now);
        var ret = ((IMethodCallMessage)msg).MethodBase.Invoke(_obj, null);
        Console.WriteLine("{0}:Invoke后", DateTime.Now);
        return new ReturnMessage(ret, null, 0, nullnull);
    }
}

执行

1
2
3
4
5
6
7
8
9
10
static void Main(string[] args)
{
    var per = new Proxy<Person>(new Person()).GetTransparentProxy() as Person;
    if (per != null)
    {
        var str = per.Say();
        Console.WriteLine("返回值:" + str);
    }
    Console.ReadKey();
}

AOP框架

AOP有动态代理和静态IL织入.

本节主要介绍动态代理方式,静态可参考PostSharp.

Castle Core

原理:本质是创建继承原来类的代理类.重写虚方法实现AOP功能.

只需引用:

Install-Package Castle.Core

(在Castle的2.5以上版本,已经将 Castle.DynamicProxy2.dll 里有内容,集成到 Castle.Core.dll 中。)

Simple Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public abstract class Person
{
    public virtual void SayHello()
    {
        Console.WriteLine("我是{0}方法""SayHello");
    }
 
    public virtual void SayName(string name)
    {
        Console.WriteLine("我是{0}方法,参数值:{1}""SayName", name);
    }
 
    public abstract void AbstactSayOther();
 
    public void SayOther()
    {
        Console.WriteLine("我是{0}方法""SayOther");
    }
}

interceptor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class SimpleInterceptor : StandardInterceptor
{
    protected override void PreProceed(IInvocation invocation)
    {
        Console.WriteLine("拦截器调用方法前,方法名是:{0}。", invocation.Method.Name);
    }
 
    protected override void PerformProceed(IInvocation invocation)
    {
        Console.WriteLine("拦截器开始调用方法,方法名是:{0}。", invocation.Method.Name);
        var attrs = invocation.MethodInvocationTarget.Attributes.HasFlag(MethodAttributes.Abstract);//过滤abstract方法
        if (!attrs)
        {
            base.PerformProceed(invocation);//此处会调用真正的方法 invocation.Proceed();
        }
    }
 
    protected override void PostProceed(IInvocation invocation)
    {
        Console.WriteLine("拦截器调用方法后,方法名是:{0}。", invocation.Method.Name);
    }
}

Main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
static void Main(string[] args)
{
    var generator = new ProxyGenerator();       //实例化【代理类生成器】 
    var interceptor = new SimpleInterceptor();  //实例化【拦截器】 
 
    //使用【代理类生成器】创建Person对象,而不是使用new关键字来实例化 
    var person = generator.CreateClassProxy<Person>(interceptor);
 
    Console.WriteLine("当前类型:{0},父类型:{1}", person.GetType(), person.GetType().BaseType);
    Console.WriteLine();
 
    person.SayHello();//拦截
    Console.WriteLine();
 
    person.SayName("Never、C");//拦截
    Console.WriteLine();
 
    person.SayOther();//普通方法,无法拦截    
    person.AbstactSayOther();//抽象方法,可以拦截    
 
    Console.ReadLine();
 
}

Castle Windsor

特性式AOP

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface IPerson
{
    void Say();
}
 
[Interceptor(typeof(LogInterceptor))]
public class Person : IPerson
{
    public void Say()
    {
        Console.WriteLine("Person's Say Method is called!");
    }
}
1
2
3
4
5
6
7
8
9
public class LogInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("{0}:拦截{1}方法{2}前,", DateTime.Now.ToString("O"), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
        invocation.Proceed();
        Console.WriteLine("{0}:拦截{1}方法{2}后,", DateTime.Now.ToString("O"), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
    }
}
1
2
3
4
5
6
7
8
9
10
11
static void Main(string[] args)
{
    using (var container = new WindsorContainer())
    {
        container.Register(Component.For<Person, IPerson>());
        container.Register(Component.For<LogInterceptor, IInterceptor>());
        var person = container.Resolve<IPerson>();
        person.Say();
    }
    Console.ReadKey();
}

非侵入式AOP

1
2
3
4
5
6
7
8
9
10
11
12
public interface IPerson
{
    void Say();
}
 
public class Person : IPerson
{
    public void Say()
    {
        Console.WriteLine("Person's Say Method is called!");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
internal static class LogInterceptorRegistrar
{
    public static void Initialize(WindsorContainer container)
    {
        container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
    }
 
    private static void Kernel_ComponentRegistered(string key, IHandler handler)
    {
        handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(LogInterceptor)));
    }
}
public class LogInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("{0}:拦截{1}方法{2}前,", DateTime.Now.ToString("O"), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
        invocation.Proceed();
        Console.WriteLine("{0}:拦截{1}方法{2}后,", DateTime.Now.ToString("O"), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
static void Main(string[] args)
{
    using (var container = new WindsorContainer())
    {
        container.Register(Component.For<IInterceptor, LogInterceptor>());//先注入拦截器
        LogInterceptorRegistrar.Initialize(container);
        container.Register(Component.For<IPerson, Person>());
        var person = container.Resolve<IPerson>();
        person.Say();
    }
    Console.ReadKey();
}

Autofac

Install-Package Autofac.Aop

通过特性标签绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class LogInterceptor : IInterceptor
 {
     public void Intercept(IInvocation invocation)
     {
         Console.WriteLine("{0}:拦截{1}方法{2}前,", DateTime.Now.ToString("O"), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
         invocation.Proceed();
         Console.WriteLine("{0}:拦截{1}方法{2}后,", DateTime.Now.ToString("O"), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
     }
 }
 
 public interface IPerson
 {
     void Say();
 }
 
 [Intercept(typeof(LogInterceptor))]
 public class Person : IPerson
 {
     public void Say()
     {
         Console.WriteLine("Person's Say Method is called!");
     }
 }

启用拦截器执行

1
2
3
4
5
6
7
8
9
10
11
static void Main(string[] args)
{
    var builder = new ContainerBuilder();
    builder.RegisterType<Person>().As<IPerson>().EnableInterfaceInterceptors();
    builder.RegisterType<LogInterceptor>();
    using (var container = builder.Build())
    {
        container.Resolve<IPerson>().Say();
    }
    Console.ReadLine();
}

或采用非侵入性方法(去掉class上的特性仍可以)

1
2
3
4
5
6
7
8
9
10
11
static void Main(string[] args)
{
    var builder = new ContainerBuilder();
    builder.RegisterType<Person>().As<IPerson>().EnableInterfaceInterceptors().InterceptedBy(typeof(LogInterceptor));
    builder.RegisterType<LogInterceptor>();
    using (var container = builder.Build())
    {
        container.Resolve<IPerson>().Say();
    }
    Console.ReadLine();
}

  

Unity

Unity默认提供了三种拦截器:TransparentProxyInterceptor、InterfaceInterceptor、VirtualMethodInterceptor。

TransparentProxyInterceptor:代理实现基于.NET Remoting技术,它可拦截对象的所有函数。缺点是被拦截类型必须派生于MarshalByRefObject。

InterfaceInterceptor:只能对一个接口做拦截,好处时只要目标类型实现了指定接口就可以拦截。

VirtualMethodInterceptor:对virtual函数进行拦截。缺点是如果被拦截类型没有virtual函数则无法拦截,这个时候如果类型实现了某个特定接口可以改用

Install-Package Unity.Interception

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyHandler : ICallHandler
{
    public int Order { getset; }//这是ICallHandler的成员,表示执行顺序
    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine("方法执行前");
        //这之前插入方法执行前的处理
        var retvalue = getNext()(input, getNext);//在这里执行方法
        //这之后插入方法执行后的处理
        Console.WriteLine("方法执行后");
        return retvalue;
    }
}
1
2
3
4
5
6
7
public class MyHandlerAttribute : HandlerAttribute
{
    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        return new MyHandler();//返回MyHandler
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public interface IPerson
{
    void Say();
}
 
[MyHandler]
public class Person : IPerson
{
    public virtual void Say()
    {
        Console.WriteLine("Person's Say Method is called!");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void Main(string[] args)
{
    using (var container = new UnityContainer())
    {
        container.AddNewExtension<Interception>();
        //1.TransparentProxyInterceptor
        //container.Configure<Interception>().SetInterceptorFor<IPerson>(new TransparentProxyInterceptor());
 
        //2.InterfaceInterceptor (使用1,2,3均可,这种侵入性最小)
        container.Configure<Interception>().SetInterceptorFor<IPerson>(new InterfaceInterceptor());
 
        //3.VirtualMethodInterceptor
        //container.Configure<Interception>().SetInterceptorFor<Person>(new VirtualMethodInterceptor());
        container.RegisterType<IPerson, Person>();
        container.Resolve<IPerson>().Say();
    }
    Console.ReadKey();
}

AOP原理解析及Castle、Autofac、Unity框架使用的更多相关文章

  1. [Solution] DI原理解析及Castle、Unity框架使用

    本节目录 DI介绍 DI基本原理 DI框架 Castle Windsor Unity Autofac Spring.Net DI介绍 控制反转(Inversion of Control,英文缩写为Io ...

  2. [Solution] AOP原理解析及Castle、Autofac、Unity框架使用

    本节目录: AOP介绍 AOP基本原理 AOP框架 Castle Core Castle Windsor Autofac Unity AOP介绍 面向切面编程(Aspect Oriented Prog ...

  3. Spring笔记(3) - debug源码AOP原理解析

    案例 @EnableAspectJAutoProxy//开启基于注解的aop模式 @Configuration public class AOPConfig { //业务逻辑类加入容器中 @Bean ...

  4. Spring AOP原理解析

    原文链接请参见:http://blog.csdn.net/u010723709/article/details/47839307

  5. [置顶] 滴滴插件化框架VirtualAPK原理解析(一)之插件Activity管理

    上周末,滴滴与360都开源了各自的插件化框架,VirtualAPK与RePlugin,作为一个插件化方面的狂热研究者,在周末就迫不及待的下载了Virtualapk框架来进行研究,本篇博客带来的是Vir ...

  6. ABP中动态WebAPI原理解析

    ABP中动态WebAPI原理解析 动态WebAPI应该算是ABP中最Magic的功能之一了吧.开发人员无须定义继承自ApiController的类,只须重用Application Service中的类 ...

  7. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  8. Spring IOC设计原理解析:本文乃学习整理参考而来

    Spring IOC设计原理解析:本文乃学习整理参考而来 一. 什么是Ioc/DI? 二. Spring IOC体系结构 (1) BeanFactory (2) BeanDefinition 三. I ...

  9. Spring源码分析之AOP从解析到调用

    正文: 在上一篇,我们对IOC核心部分流程已经分析完毕,相信小伙伴们有所收获,从这一篇开始,我们将会踏上新的旅程,即Spring的另一核心:AOP! 首先,为了让大家能更有效的理解AOP,先带大家过一 ...

随机推荐

  1. python操作docx文档(转)

    python操作docx文档 关于python操作docx格式文档,我用到了两个python包,一个便是python-docx包,另一个便是python-docx-template;,同时我也用到了很 ...

  2. (转)libvirt API的基本概念

    本文摘自:http://blog.sina.com.cn/s/blog_da4487c40102v31i.html libvirt对象 libvirt的对象向外展现了虚拟化环境的所有资源.libvir ...

  3. Linux实战教学笔记36:PHP服务缓存加速深度优化实践

    一,PHP缓存加速器介绍与环境准备 1.1 PHP缓存加速器介绍 1.1.1 操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Ope ...

  4. preview放大镜

    [preview放大镜] preview有放大镜功能,放`键即可(键盘左上角,1左边的键).

  5. golang怎么使用redis,最基础的有效的方法

    最近在学GO语言,我自己也喜欢使用redis,于是乎就顺便把go操作redis的方法也给学了,有个第三方包,在GitHub上面找的 go get github.com/alphazero/Go-Red ...

  6. centos6.5 源码安装 mysql

    1.下载源码包 我的版本:mysql-5.6.4-m7.tar.gz 2.安装之前先卸载CentOS自带的MySQL [root@localhost ~]# yum remove mysql 3.编译 ...

  7. Eclipse设置Tab键缩进4个空格的步骤,也就是按一下Tab键输出四个空格

    Eclipse设置Tab键缩进4个空格的步骤,也就是按1下Tab键输出4个空格,步奏如下 1.点击 window->preference-,选择 General->Editors-> ...

  8. ArcGIS js api三种查询功能

    转自https://blog.csdn.net/lovecarpenter/article/details/52669777

  9. windows版本免安装redis, nginx, zookeeper

    redis官网:https://redis.io/ windows版本免安装redis下载链接:https://github.com/MSOpenTech/redis/releases nginx官网 ...

  10. 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)

    [抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...