几种Aop实现及Castle.DynamicProxy的使用
简单实现
通过继承实现
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的使用的更多相关文章
- Castle DynamicProxy基本用法(AOP)
本文介绍AOP编程的基本概念.Castle DynamicProxy(DP)的基本用法,使用第三方扩展实现对异步(async)的支持,结合Autofac演示如何实现AOP编程. AOP 百科中关于AO ...
- 使用Castle DynamicProxy (AOP)
在本文中,我将引导您了解.NET环境中的面向方面编程(AOP)概念,以及如何使用Castle DynamicProxy创建和附加方面.在我们开始之前,让我快速介绍AOP和 IoC.如果您已经熟悉这些 ...
- 在 CAP 中使用 AOP ( Castle.DynamicProxy )
简介 本篇文章主要介绍如何在 CAP 中集成使用 Castle.DynamicProxy,Castle DynamicProxy 是一个用于在运行时动态生成轻量级.NET代理的库.代理对象允许在不修改 ...
- 基于Autofac, Castle.DynamicProxy的动态WCF解决方案(原创)
本方案解决了下面3个主要的问题: 1.减少配置,为了避免每次新增service都需要去修改配置文件,包括服务器端跟各个客户端的. 2.能够使用函数重载,泛型函数,以及泛型类. 3.使项目能够快速地在w ...
- Castle.DynamicProxy Part 1: ClassProxy
1.Castle中代理对象的分类 总的来说,代理对象大概可以分为2大类: 1.继承类型的代理对象 一类是继承类型的代理类.即:有一个类A,它的代理类是B.B是继承自A的.调用代理类B中的方法时,可以通 ...
- castle.dynamicProxy学习笔记
目的: 可以将castle.dynamicProxy当成代码生成器,快速的生成自己想的代码.这个库经历了这么多年的测试,应该可以用了:D 概念: IInterceptor:拦截器 当方法(属性的本质是 ...
- Castle DynamicProxy
Introduction¶ Castle DynamicProxy is a library for generating lightweight .NET proxies on the fly at ...
- Aop之使用Castle动态代理实现对方法的拦截
using System; using System.Linq; using Castle.DynamicProxy; namespace AopTest { class AopTest { stat ...
- AOP之Castle DynamicProxy 动态代理
这里主要介绍使用castle这个动态代理,在.net一些开源的框架里可以找到它的影子,就连微软的rchard也是使用这个进行方法拦截等可以基于这个进行方法拦截,在这个方面PostSharp算是比较好用 ...
随机推荐
- 谈谈 Google 的 Test Certified
转载请联系作者,谢谢! 本文简单介绍下Google的Test Certified. Test Certified(后文简称TC)是Google内部的一个认证项目,在8年的时间里取得了多个里程碑,有17 ...
- POJ3260The Fewest Coins[背包]
The Fewest Coins Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6299 Accepted: 1922 ...
- java list
List:元素是有序的(怎么存的就怎么取出来,顺序不会乱),元素可以重复(角标1上有个3,角标2上也可以有个3)因为该集合体系有索引 ArrayList:底层的数据结构使用的是数组结构(数组长度是可变 ...
- Asp.Net MVC中DropDownListFor的用法(转)
2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T List<T>的第一个 ...
- 兼容IE678的placeholder
jquery-placeholder.js是基于jquery的插件,对不支持placeholder的浏览器有非常好的兼容性,只需引入这个js文件,然后给所有Input元素调用一下placeholder ...
- js获取可视区大小和页面大小的兼容性写法
var getPageSize = function() { var scrW, scrH; if(window.innerHeight && window.scrollMaxY) { ...
- workqueue机制分析之process_one_work分析
工作者线程不断执行,从work_poll结构中卸下一个work, 然后进入函数process_one_work 来执行这个work. process_one_work(struct worker *w ...
- 阿里云Center OS 6.2 Nginx 配置 SSL/TLS HTTPS配置
1.通过https://www.startssl.com/ 免费SSL证书 STARTSSL 跟VeriSign一样,StartSSL(网址:http://www.startssl.com,公司名:S ...
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】(转)
1.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将各个业务已动态链接库的形式加载进来,这就是所谓的插件.linux提供了加载和处理动态链接库的系统 ...
- C#并发编程经典实例--笔记
一.简介 --并发 同时做多件事情 --多线程 并发的一种形式,它采用多个线程来执行程序. **如非必要,代码里不要出现 "new ...