.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. NOIP1999邮票面值设计[搜索|DP]

    题目描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤40)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大值MAX,使在1-MAX之间的每一个邮资值都能得到 ...

  2. TortoiseSVN status cache占用CPU高

    进程占用CPU高 每次从SVN上更新资源时,电脑都会卡死,直到资源更新完.当要Commit资源时,SVN也会卡死资源管理器,如下图所示: 解决占用CPU高的问题 1.禁用图标缓存 2.排除路径和包含路 ...

  3. Android app 简单的电话拨号器

    实现步骤: 1.画UI 可以用拖拽和文本编辑. 2.根据UI写业务逻辑  在MainActivity中的onCreate中编写 //get editText content et_number = ( ...

  4. NOIP2013 花匠

    题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希 望剩下的花排列得比较别致. 具 ...

  5. HTML 学习笔记 JavaScript(数据类型)

    字符串 数字 布尔 数组 对象 Null Undefined JavaScript 拥有动态类型 JavaScript拥有动态类型 这意味着相同的变量可用作不同的类型: 实例: var x // x ...

  6. Java核心技术点之注解

    本博文是对Java中注解相关知识点的简单总结,若有叙述不清晰或是不准确的地方,希望大家可以指正,谢谢大家:) 一.什么是注解 我们大家都知道Java代码中使用注释是为了向以后阅读这份代码的人解释说明一 ...

  7. jQuery的prop和attr方法之间区别

    JQuery.attr(): Get the value of an attribute for the first element in the set of matched elements. J ...

  8. Windows 8.1 新增控件之 AppBar

    Windows 8.1 与Windows 8 相比已经有了很多改进,从ITPro 角度这篇文章<What's New in Windows 8.1>已经表述的很详细.对开发者来说,最明显的 ...

  9. 韩国网页设计资料《网页设计大师2》JPG+PSD+TXT等 73.89G 百度云下载

    < 网页设计大师2 >超越第一代版本,提供更新更精美的网页素材模板.全部由国际顶级设计师精选打造,完全展示走在潮流 之前的设计风格.是网页设计师/UI交互界面设计师必备工具. < 网 ...

  10. C/C++实践笔记_002编译和链接

    1.要卡死程序用异步,同步的话开一个就关一个值为非0死循环.预处理优先于编译,别称预编译main函数死循环2.程序总是从main函数开始执行的C语言本身不提供输入输出语句print等来自于stdio库 ...