.Net平台AOP技术研究

简单实现

通过继承实现

public interface ICoding
{
void DoSth();
}
public class Coding : ICoding
{
public virtual void DoSth()
{
Console.WriteLine("敲代码咯!");
}
}
public class ProjectDevelopment : Coding, ICoding
{
public override void DoSth()
{
before();
base.DoSth();
after();
} private void before()
{
Console.WriteLine("需求分析!");
}
private void after()
{
Console.WriteLine("测试!");
}
}

  

装饰者(由继承改作组合)

public interface ICoding
{
void DoSth();
}
public class Coding : ICoding
{
public virtual void DoSth()
{
Console.WriteLine("敲代码咯!");
}
}
public class ProjectDevelopment : ICoding
{
private ICoding coding;
public ProjectDevelopment(ICoding coding)
{
this.coding = coding;
}
public void DoSth()
{
before();
coding.DoSth();
after();
} private void before()
{
Console.WriteLine("需求分析!");
}
private void after()
{
Console.WriteLine("测试!");
}
}

  

ASP.NET MVC中的拦截器

public interface IAdditionalOp
{
void before();
void after();
}
public class AdditionalOp : Attribute, IAdditionalOp
{
public void before()
{
Console.WriteLine("需求分析!");
}
public void after()
{
Console.WriteLine("测试!");
}
} public class Coding
{
[AdditionalOp]
public void DoSth()
{
Console.WriteLine("敲代码咯!");
}
} public class ProjectDevelopment
{
public void Development()
{
var classUnder = typeof(Coding);
var allMethods = classUnder.GetMethods();
var methodUnder = allMethods.Where(m => m.Name == "DoSth"); foreach (MethodInfo methodInfo in methodUnder)
{
Attribute[] attributes = Attribute.GetCustomAttributes(methodInfo, typeof(AdditionalOp));
foreach (var item in attributes)
{
(item as AdditionalOp).before();
} Coding coding = new Coding();
coding.DoSth(); foreach (var item in attributes)
{
(item as AdditionalOp).after();
}
}
}
}

  

动态代理

动态代理,就是在运行时动态地产生代理类,进而产生代理对象。

项目中导入Castle.Core程序包

5种动态代理的生成模式

public interface ICoding
{
void DoSth();
}
public class Coding : ICoding
{
public virtual void DoSth()
{
Console.WriteLine("敲代码咯!");
}
}
public class Architecture : Coding
{
public override void DoSth()
{
Console.WriteLine("架构设计!");
base.DoSth();
}
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
before();
if (invocation.InvocationTarget != null)
{
invocation.Proceed();
}
after();
} private void before()
{
Console.WriteLine("需求分析!");
}
private void after()
{
Console.WriteLine("测试!");
}
} public class MyInterceptor2 : IInterceptor
{
public void Intercept(IInvocation invocation)
{
before();
if (invocation.InvocationTarget != null)
{
invocation.Proceed();
}
after();
} private void before()
{
Console.WriteLine("需求分析2!");
}
private void after()
{
Console.WriteLine("测试2!");
}
}
public interface IAddItem
{
void AddItem();
} public class AnotherItem : IAddItem
{
public void AddItem()
{
Console.WriteLine("另一个项目开始了!");
}
}

  

class Program
{
static void Main(string[] args)
{
ClassProxy();
ClassProxyWithTarget();
InterfaceProxyWithoutTarget();
InterfaceProxyWithTarget();
InterfaceProxyWithTargetInterface();
Mixin();
Mixin2();
Mixin3();
Mixin4();
Mixin5();
Console.ReadKey();
} static void ClassProxy()
{
Console.WriteLine("\n*************ClassProxy*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateClassProxy<Coding>(
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void ClassProxyWithTarget()
{
Console.WriteLine("\n*************ClassProxyWithTarget*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateClassProxyWithTarget<Coding>(
new Architecture(),
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void InterfaceProxyWithoutTarget()
{
Console.WriteLine("\n*************InterfaceProxyWithoutTarget*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateInterfaceProxyWithoutTarget<ICoding>(
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void InterfaceProxyWithTarget()
{
Console.WriteLine("\n*************InterfaceProxyWithTarget*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateInterfaceProxyWithTarget<ICoding>(
new Architecture(),
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
} static void InterfaceProxyWithTargetInterface()
{
Console.WriteLine("\n*************InterfaceProxyWithTargetInterface*************\n");
var generator = new ProxyGenerator();
var ProjectDevelopment = generator.CreateInterfaceProxyWithTargetInterface<ICoding>(
new Architecture(),
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth(); Print(ProjectDevelopment);
}
static void Print(object o)
{
Console.WriteLine();
Console.WriteLine("GetType():".PadRight(30) + o.GetType());
Console.WriteLine("GetType().BaseType:".PadRight(30) + o.GetType().BaseType); var compositeField = o.GetType().GetField("__target");
Console.WriteLine("__target:".PadRight(30) + compositeField?.FieldType + ", " + compositeField?.Name); foreach (var interfaceType in o.GetType().GetInterfaces())
{
Console.WriteLine("GetType().GetInterfaces():".PadRight(30) + interfaceType);
} foreach (var a in (o as IProxyTargetAccessor).GetInterceptors())
{
Console.WriteLine("GetInterceptors():".PadRight(30) + a);
}
} static void Print2(object o)
{
Console.WriteLine();
Console.WriteLine("GetType():".PadRight(30) + o.GetType());
Console.WriteLine("GetType().BaseType:".PadRight(30) + o.GetType().BaseType); var compositeField = o.GetType().GetField("__target");
Console.WriteLine("__target:".PadRight(30) + compositeField?.FieldType + ", " + compositeField?.Name); foreach (var field in o.GetType().GetFields())
{
if (field.Name.StartsWith("__mixin"))
{
Console.WriteLine("GetType().GetFields():".PadRight(30) + field?.FieldType + ", " + field?.Name);
}
} foreach (var interfaceType in o.GetType().GetInterfaces())
{
Console.WriteLine("GetType().GetInterfaces():".PadRight(30) + interfaceType);
} foreach (var a in (o as IProxyTargetAccessor).GetInterceptors())
{
Console.WriteLine("GetInterceptors():".PadRight(30) + a);
}
} static void Mixin()
{
Console.WriteLine("\n*************CreateClassProxy Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateClassProxy<Coding>(
options,
new MyInterceptor(),
new MyInterceptor2()
);
ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin2()
{
Console.WriteLine("\n*************CreateClassProxyWithTarget Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateClassProxyWithTarget<Coding>(
new Architecture(),
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin3()
{
Console.WriteLine("\n*************CreateInterfaceProxyWithoutTarget Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateInterfaceProxyWithoutTarget<ICoding>(
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin4()
{
Console.WriteLine("\n*************CreateClassProxyWithTarget Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateClassProxyWithTarget<Coding>(
new Architecture(),
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
} static void Mixin5()
{
Console.WriteLine("\n*************CreateInterfaceProxyWithTargetInterface Mixin*************\n");
var generator = new ProxyGenerator();
var options = new ProxyGenerationOptions();
options.AddMixinInstance(new AnotherItem()); var ProjectDevelopment = generator.CreateInterfaceProxyWithTargetInterface<ICoding>(
new Architecture(),
options,
new MyInterceptor(),
new MyInterceptor2()
); ProjectDevelopment.DoSth();
Console.WriteLine("\n");
(ProjectDevelopment as IAddItem).AddItem(); Print2(ProjectDevelopment);
}
}

  

几种Aop实现及Castle.DynamicProxy的使用的更多相关文章

  1. Castle DynamicProxy基本用法(AOP)

    本文介绍AOP编程的基本概念.Castle DynamicProxy(DP)的基本用法,使用第三方扩展实现对异步(async)的支持,结合Autofac演示如何实现AOP编程. AOP 百科中关于AO ...

  2. 使用Castle DynamicProxy (AOP)

    在本文中,我将引导您了解.NET环境中的面向方面编程(AOP)概念,以及如何使用Castle DynamicProxy创建和附加方面.在我们开始之前,让我快速介绍AOP和  IoC.如果您已经熟悉这些 ...

  3. 在 CAP 中使用 AOP ( Castle.DynamicProxy )

    简介 本篇文章主要介绍如何在 CAP 中集成使用 Castle.DynamicProxy,Castle DynamicProxy 是一个用于在运行时动态生成轻量级.NET代理的库.代理对象允许在不修改 ...

  4. 基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)

    本方案解决了下面3个主要的问题: 1.减少配置,为了避免每次新增service都需要去修改配置文件,包括服务器端跟各个客户端的. 2.能够使用函数重载,泛型函数,以及泛型类. 3.使项目能够快速地在w ...

  5. Castle.DynamicProxy Part 1: ClassProxy

    1.Castle中代理对象的分类 总的来说,代理对象大概可以分为2大类: 1.继承类型的代理对象 一类是继承类型的代理类.即:有一个类A,它的代理类是B.B是继承自A的.调用代理类B中的方法时,可以通 ...

  6. castle.dynamicProxy学习笔记

    目的: 可以将castle.dynamicProxy当成代码生成器,快速的生成自己想的代码.这个库经历了这么多年的测试,应该可以用了:D 概念: IInterceptor:拦截器 当方法(属性的本质是 ...

  7. Castle DynamicProxy

    Introduction¶ Castle DynamicProxy is a library for generating lightweight .NET proxies on the fly at ...

  8. Aop之使用Castle动态代理实现对方法的拦截

    using System; using System.Linq; using Castle.DynamicProxy; namespace AopTest { class AopTest { stat ...

  9. AOP之Castle DynamicProxy 动态代理

    这里主要介绍使用castle这个动态代理,在.net一些开源的框架里可以找到它的影子,就连微软的rchard也是使用这个进行方法拦截等可以基于这个进行方法拦截,在这个方面PostSharp算是比较好用 ...

随机推荐

  1. Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]

    题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...

  2. java 常见数据类型

    int 4字节 -2 147 483 648-2 147 483 647(正好超过20亿) short 2字节 -32 768-32 767 long 8字节 -9 223 372 036 854 7 ...

  3. AC日记——欧几里得的游戏 洛谷 P1290

    题目描述 欧几里德的两个后代Stan和Ollie正在玩一种数字游戏,这个游戏是他们的祖先欧几里德发明的.给定两个正整数M和N,从Stan开始,从其中较大的一个数,减去较小的数的正整数倍,当然,得到的数 ...

  4. AC日记——计算2的N次方 openjudge 1.6 12

    12:计算2的N次方 总时间限制:  1000ms 内存限制:  65536kB 描述 任意给定一个正整数N(N<=100),计算2的n次方的值. 输入 输入一个正整数N. 输出 输出2的N次方 ...

  5. MVC调用SVC无法找到资源解决问题

    webconfig配置下就可以,但MVC当中老是报错 404 not found.解决办法: routes.IgnoreRoute("{resource}.svc/{*pathInfo}&q ...

  6. [No000065]python 获取当前时间

    要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间.用下面的方式来取得当前时间的时间戳: import time print(time.time()) 输出的结果是 ...

  7. PPT文档页数显示的增加和更新

    在PPT的右下角增加页数的显示能够帮助演讲者把握进度,所以会经常遇到需要把页数显示在右下角的情况,这次在制作ppt的时候也遇到了.因此在这里总结一下设置方法. 一.在右下角显示当前页数和总页数 1)获 ...

  8. PAT 1003. 我要通过!(20) JAVA

    参考http://blog.csdn.net/bin8632/article/details/50216297 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答 ...

  9. BZOJ 1066 【SCOI2007】 蜥蜴

    Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为$1$,蜥蜴的跳跃距离是d,即蜥蜴可以跳 ...

  10. swift 随机生成背景颜色

    swift是一门新语言,相关的文档资料现在基本上还不是很完整.在尝试开发过程中,走了不少弯路.在这里记录一下自己的”路“,希望以后能少走弯路. 生成随机背景颜色使用的语法和C#或者JAVA基本一致. ...