本文关注以下方面(环境为VS2012、.Net Framework 4.5以及Unity 3):
- AOP简介;
- Interception using Unity示例
- 配置文件示例
一、AOP简介
AOP为Aspect-Oriented Programming的缩写,意为"面向切面(方面)编程",按维基百科的解释是"AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns" ()。
通俗的理解就是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术,我们可以理解为在一个服务的流程中,插入与该服务的业务逻辑无关的系统服务逻辑(操作前或操作后),如日志记录、安全认证、性能检测等等。
二、Interception using Unity示例
本示例中主要用于展示如何应用Unity实现编程的Interception,更多内容请阅读
还是以一个简单的日志记录程序为例
Console程序如下(以Log为例)
interface ILog { void Log(string message); }
class ConsoleLog : ILog { public void Log(string message) { Console.WriteLine(message); } }
下面我们编写一个切入的用于记录日志的类,当然首先要引入相应的命名空间,Unity Interception Extension可以通过NuGet获取
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Practices.Unity.InterceptionExtension;namespace Demo{ class LoggingInterceptionBehavior : IInterceptionBehavior { public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { // Before invoking the method on the original target. WriteLog(String.Format("Invoking method {0} at {1}", input.MethodBase, DateTime.Now.ToLongTimeString())); // Invoke the next behavior in the chain. var result = getNext()(input, getNext); // After invoking the method on the original target. if (result.Exception != null) { WriteLog(String.Format("Method {0} threw exception {1} at {2}", input.MethodBase, result.Exception.Message, DateTime.Now.ToLongTimeString())); } else { WriteLog(String.Format("Method {0} returned {1} at {2}", input.MethodBase, result.ReturnValue, DateTime.Now.ToLongTimeString())); } return result; } public IEnumerableGetRequiredInterfaces() { return Type.EmptyTypes; } public bool WillExecute { get { return true; } } private void WriteLog(string message) { Console.WriteLine(message); } }}
为了方便观察多个切入,我们在定义一个PerformanceInterceptionBehavior
using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Practices.Unity.InterceptionExtension;namespace PCT.Unity.DIWithUntiy{ class PerformanceInterceptionBehavior : IInterceptionBehavior { public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Reset(); stopwatch.Start(); // Invoke the next behavior in the chain. var result = getNext()(input, getNext); // After invoking the method on the original target. if (result.Exception != null) { WriteLog(String.Format("Method {0} threw exception {1} at {2}", input.MethodBase, result.Exception.Message, DateTime.Now.ToLongTimeString())); } else { stopwatch.Stop(); WriteLog(String.Format("Method {0} executed {1}", input.MethodBase, stopwatch.Elapsed)); } return result; } public IEnumerableGetRequiredInterfaces() { return Type.EmptyTypes; } public bool WillExecute { get { return true; } } private void WriteLog(string message) { Console.WriteLine(message); } }}
最终控制台的代码如下
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Practices.Unity;using Microsoft.Practices.Unity.InterceptionExtension;namespace PCT.Unity.DIWithUntiy{ class Program { static void Main(string[] args) { var container = new UnityContainer(); container.AddNewExtension(); container.RegisterType ( new Interceptor (), new InterceptionBehavior (), new InterceptionBehavior ()); var logger = container.Resolve (); logger.Log("Hello, Unity Framework!"); Console.ReadKey(); } }}
运行结果如下
如果我们把代码改成如下
container.RegisterType( new Interceptor (), new InterceptionBehavior (), new InterceptionBehavior ());
再次运行
是不是发现了什么,对,切入的顺序不同,最终执行的顺序也不同。
三、配置文件示例
控制台代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Practices.Unity;using Microsoft.Practices.Unity.Configuration;using Microsoft.Practices.Unity.InterceptionExtension;namespace PCT.Unity.DIWithUntiy{ class Program { static void Main(string[] args) { var container = new UnityContainer(); //container.AddNewExtension(); //container.RegisterType ( // new Interceptor (), // new InterceptionBehavior (), // new InterceptionBehavior ()); container.LoadConfiguration(); var logger = container.Resolve (); logger.Log("Hello, Unity Framework!"); Console.ReadKey(); } }}
配置文件