Unity.Interception(AOP)
/// <summary>
/// 接口
/// </summary>
public interface ITalk
{
void talk(string msg);
}
2.业务实现
public class PeopleTalk : ITalk
{
private string username; private int age; public string UserName
{
get { return username; }
} public int Age
{
get { return age; }
} public PeopleTalk(string userName, int age)
{
this.username = userName;
this.age = age;
} public virtual void talk(string msg)
{
Console.WriteLine(msg + "!你好,我是" + username + ",我的年龄" + age);
} }
3.代理对象
public class TalkProxy : ITalk
{
private ITalk talker; public TalkProxy(ITalk talker)
{
this.talker = talker;
} public void talk(string msg)
{
talker.talk(msg);
} public void talk(string msg, string singName)
{
talker.talk(msg);
sing(singName);
} public void sing(string singName)
{
Console.WriteLine("唱歌:" + singName);
}
}
4.调用
class Program
{
static void Main(string[] args)
{
#region 静态代理 ITalk people = new PeopleTalk("AOP", ); people.talk("No ProXY Test!"); Console.WriteLine("---------------------------------"); TalkProxy talker = new TalkProxy(people); talker.talk("ProXY Test", "代理"); #endregion }
}
代理模式是一种简单的AOP,talk是一个切面,我们可以在代理类中添加日志、校验、异常处理等等。这样我们就实现了,核心关注点与横切关注点的分离。正如Avanade公司的高级方案架构师Adam Magee所说,AOP的核心思想就是”将应用程序中的商业逻辑同对其提供支持的通用服务进行分离“。
/// <summary>
/// Unity为我们提供了一个IInterceptionBehavior接口需要实现这个接口
/// 接口为我们提供了三个方式(GetRequiredInterfaces、Invoke、WillExecute)实现
/// WillExecute表示是否执行该行为,如果是false这个方法被调用时,不会被捕捉。因为我们总是要执行的,所以为true
/// GetRequiredInterfaces将你想要的接口类型和行为联系起来,我们暂时不需要,所以返回Type.EmptyTypes
/// Invoke执行方式接口
/// </summary>
public class LoggingInterceptionBehavior : IInterceptionBehavior
{
public bool WillExecute
{
get { return true; }
} public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
} public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("Method: {0}", input.MethodBase.Name); Console.WriteLine("参数:");
for (var i = ; i < input.Arguments.Count; i++)
{
Console.WriteLine("{0}: {1}", input.Arguments.ParameterName(i), input.Arguments[i]);
}
Console.WriteLine("执行前");
var result = getNext()(input, getNext);//在这里执行方法
if (result.Exception != null)
{
//发生错误记录日志
Console.WriteLine(String.Format("Method {0} threw exception {1} at {2}", input.MethodBase, result.Exception.Message, DateTime.Now.ToLongTimeString()));
}
Console.WriteLine("执行后");
return result;
}
}
2.调用
class Program
{
static void Main(string[] args)
{
UnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<ITalk, PeopleTalk>(
new InjectionConstructor("AOP", ),
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<LoggingInterceptionBehavior>());
ITalk talker = container.Resolve<ITalk>();
talker.talk("ProXY Test!");
}
}
以上基本完成了简单的Unity.Interception
当然在Unity中不只有InterfaceInterceptor一种拦截器,它还包含其它两种拦截器:TransparentProxyInterceptor 与 VirtualMethodInterceptor
这里就不详细介绍就提一下这三种:
Unity.Interception(AOP)的更多相关文章
- 面向切面编程(AOP)及其作用
在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用. 1.面向切面编程(AOP) 面向切面编程(AOP)就是对软件系统不同关注点的分离,开发者通过拦截方法调用并在方法调用前后添加辅助代码. ...
- Atitit 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)的区别和联系
Atitit 面向对象编程(OOP).面向组件编程(COP).面向方面编程(AOP)和面向服务编程(SOP)的区别和联系 1. 面向组件编程(COP) 所以,组件比起对象来的进步就在于通用的规范的引入 ...
- 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...
- Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享
Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com ...
- 依赖注入及AOP简述(十二)——依赖注入对象的行为增强(AOP) .
四.依赖注入对象的行为增强(AOP) 前面讲到,依赖注入框架的最鲜明的特点就是能够提供受容器管理的依赖对象,并且可以对对象提供行为增强(AOP)功能,所以这一章我们来讨论有关AOP的话题. 1. ...
- 依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦(转good)
依赖注入(DI)有助于应用对象之间的解耦,而面向切面编程(AOP)有助于横切关注点与所影响的对象之间的解耦.所谓横切关注点,即影响应用多处的功能,这些功能各个应用模块都需要,但又不是其主要关注点,常见 ...
- C# 中使用面向切面编程(AOP)中实践代码整洁
1. 前言 最近在看<架构整洁之道>一书,书中反复提到了面向对象编程的 SOLID 原则(在作者的前一本书<代码整洁之道>也是被大力阐释),而面向切面编程(Aop)作为面向对象 ...
- C# 中使用面向切面编程(AOP)中实践代码整洁(转)
出处:https://www.cnblogs.com/chenug/p/9848852.html 1. 前言 最近在看<架构整洁之道>一书,书中反复提到了面向对象编程的 SOLID 原则( ...
- (转存)面向切面编程(AOP)的理解
面向切面编程(AOP)的理解 标签: aop编程 2010-06-14 20:17 45894人阅读 评论(11) 收藏 举报 分类: Spring(9) 在传统的编写业务逻辑处理代码时,我们通常 ...
随机推荐
- JavaScript - 正则表达之二
正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...
- table隔行变色
table tr:nth-child(2n) { background: #EEF8F0; } table tr:nth-child(2n+1) { b ...
- file_get_contents带bom
$dmText = file_get_contents( AROOT .'data' . DS . 'DMType.json.php'); if(preg_match('/^\xEF\xBB\xBF/ ...
- HTTP连接管理
本文是<HTTP权威指南>读书笔记: 几乎所有的HTTP通信都是通过TCP/IP承载的,当HTTP要传送一些报文时,会以流的形式将报文数据的内容通过一条打开的TCP连接按序传输.因此HTT ...
- C#高级编程笔记 2016年10月26日 MVC入门 Controller
1.MVC的定义: Models: Classes that represent the data of the application and that use validation logi ...
- 《转载》跟我学spring3
一.<跟我学spring3>电子书下载地址: <跟我学spring3> (1-7 和 8-13) http://jinnianshilongnian.iteye.com/bl ...
- 线性表Linearlist
顺序存储,链式存储,索引存储,散列存储 基本运算 SLIST 1.置空表 void SetNull(&L) 2.求长度 int Length(L) 3.取元素 ...
- Reverse Core 第二部分 - 16&17章 - 基址重定位表&.reloc节区
第16-17章 - 基址重定位表&.reloc节区 @date: 2016/11/31 @author: dlive 0x00 前言 这几天忙着挖邮箱漏洞,吃火锅,马上要被关禁闭,看书进度比较 ...
- Android studio 英文——中文 翻译插件
TranslationPlugin 1.手动下载 TranslationPlugin ,在Android studio 中 2. 3. 4.选中文件,点击OK 5.设置快捷键 代号1 : 代号2 :
- jquery的checkbox,radio,select等方法总结
jquery的checkbox,radio,和select是jquery操作的一个难点和重点,很多前端新手对其了解不是很透彻.时间久了不用,我在写的时候有时也难免对某些操作支支吾吾,记不清楚,现在,对 ...