一.   使用Unity的AOP实现

a)         整体项目截图:

b) 添加Unity的Nuget包, 直接使用最新版就行, 需要添加两个 Unity 和 Unity.Interception (这个是为AOP做的一个扩展)

c)    AOP配置文件, 详细注释(CfgFiles\Unity.Config)

  1. <!--这是一个标准Unity配置文件的格式-->
  2.  
  3. <configuration> <!--根节点名称-->
  4. <configSections>
  5. <!--name容器的名称 type 表示如何查找节点, 这是固定写法-->
  6. <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
  7. </configSections>
  8. <unity>
  9. <!--下面一行是固定写法, 用这个写法来支持AOP-->
  10. <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
  11. <containers><!--定义一组容器-->
  12. <container name="aopContainer">
  13. <!--定义一个容器; 表示在IUserProcessor这个接口下所有的方法都给他增加, 这个容器(aopContainer)内所包含的方法-->
  14. <extension type="Interception"/>
  15. <!--这里使用Interception类型的扩展; 其实在Unity中有三种类型来处理这件事; 这种是属于实现接口的方法, 还有一种是继承父类; 第三种是使用Virtual虚方法的形式, 使用虚方法有点类似于CastleProxyAOP这个代理-->
  16. <!--把接口映射到具体的实现类; 也就是说告诉容器如果遇到了MyAOP.UnityWay.IUserProcessor这个抽象, 就使用MyAOP.UnityWay.UserProcessor来帮我们实例化-->
  17. <!--MyAOP.UnityWay.IUserProcessor完整的接口名称, MyAOP是程序所在的dll名称, 程序集的名称(当然在这里是当前exe文件的名称-->
  18. <!--MyAOP.UnityWay.UserProcessor具体的实现类, MyAOP和上面的解释一样-->
  19. <!--MyAOP是程序集的名称-->
  20. <!-- register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP" 这一句整体的意思就是告诉容器, 如果遇到IUserProcessor这个抽象, 就使用UserProcessor类来帮我进行初始化一个具体的实例出来-->
  21. <register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP">
  22. <!--Unity中的AOP有三种模式, 第一种必须继承MarshalByRefObject这个父类的AOP
  23. 第二种必须是虚方法的AOP
  24. 第三种就是下面写的接口形式的 InterfaceInterceptor 一般都用这种接口形式的:
  25. 表示只要是接口下的所有方法, 都会被增加上下面的方法
  26. -->
  27. <interceptor type="InterfaceInterceptor"/> <!--AOP支持的模式; 推荐使用这种接口模式的-->
  28. <!--下面这五个表示 在IUserProcessor接口中的所有方法, 都拥有下面方法的扩展-->
  29.  
  30. <!--异常处理的; 注意如果你想全局处理异常, 则应该把异常处理的Behavior放到最顶层, 否则在Unity中自己抛出的异常则会抓不到-->
  31. <interceptionBehavior type="MyAOP.UnityWay.ExceptionLoggingBehavior, MyAOP"/>
  32. <!--缓存的behavior; 注意缓存的Behavior也应该放在记录日志之前; 需要注意的是, 如果被缓存命中了, 那么后面的AOP也不会执行的-->
  33. <interceptionBehavior type="MyAOP.UnityWay.CachingBehavior, MyAOP"/>
  34. <!--方法执行前写日志-->
  35. <interceptionBehavior type="MyAOP.UnityWay.LogBeforeBehavior, MyAOP"/>
  36. <!--参数检查-->
  37. <interceptionBehavior type="MyAOP.UnityWay.ParameterCheckBehavior, MyAOP"/>
  38. <!--方法执行后做的事情-->
  39. <interceptionBehavior type="MyAOP.UnityWay.LogAfterBehavior, MyAOP"/>
  40. </register>
  41. </container>
  42. </containers>
  43. </unity>
  44. </configuration>

d)  Model代码:

  1. public class User
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public string Password { get; set; }
  6. }

e)  IuserProcessor代码:

  1. public interface IUserProcessor
  2. {
  3. void RegUser(User user);
  4. User GetUser(User user);
  5. }

f)  UserProcessor代码:

  1. public class UserProcessor : IUserProcessor
  2. {
  3. public void RegUser(User user)
  4. {
  5. Console.WriteLine("用户已注册。");
  6. //throw new Exception("11");
  7. }
  8. [Obsolete]
  9. public User GetUser(User user)
  10. {
  11. return user;
  12. }
  13. }

g) LogBeforeBehavior代码(在方法执行之前写日志):

  1. /// <summary>
  2. /// 1. 标准的通过AOP写日志的功能, 必须实现IInterceptionBehavior; 这个接口来自于Unity容器
  3. /// </summary>
  4. public class LogBeforeBehavior : IInterceptionBehavior
  5. {
  6. /// <summary>
  7. /// 3. 此方法为固定写法
  8. /// </summary>
  9. /// <returns></returns>
  10. public IEnumerable<Type> GetRequiredInterfaces()
  11. {
  12. return Type.EmptyTypes;
  13. }
  14.  
  15. /// <summary>
  16. /// 4. 关键方法
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <param name="getNext"></param>
  20. /// <returns></returns>
  21. public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  22. {
  23. Console.WriteLine("LogBeforeBehavior");
  24. //5. 如果有的方法想用, 有的方法不想被AOP, 则可以在这里做切断
  25.  
  26. //这里可以将某个方法打上特性,然后获取MemberInfo中的特性信息, 以此来判断哪个
  27. //方法需要AOP切入,哪些方法不需要AOP切入
  28. Console.WriteLine(input.MethodBase.Name);
  29. // Console.WriteLine( input.MethodBase.GetCustomAttributes(true)[0].ToString() );
  30. foreach (var item in input.Inputs) //input中包含了所有的参数信息
  31. {
  32. Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
  33. //反射&序列化获取更多信息
  34. }
  35. // Unity.Interception.InterceptionBehaviors.InvokeInterceptionBehaviorDelegate gn = getNext();
  36. // return getNext()(input, getNext);//注意这种写法也是可以的; 这种写法的意思就是getNext()方法返回了一个委托, 然后委托又被调用了
  37. return getNext().Invoke(input, getNext);//4. 固定写法
  38.  
  39. // return null;
  40. //return gn.Invoke(input, getNext);
  41. //getNext()表示执行完当前的方法之后, 去执行原始方法; 但是注意的是, getNext()中可能有多个方法
  42. }
  43.  
  44. /// <summary>
  45. /// 2. 固定写法
  46. /// </summary>
  47. public bool WillExecute
  48. {
  49. get { return true; }
  50. }
  51. }

h)  LogAfterBehavior代码(方法执行之后写日志):

  1. //IInterceptionBehavior来自于AOP容器中 Unity.Interception.InterceptionBehaviors;
  2. public class LogAfterBehavior : IInterceptionBehavior
  3. {
  4. /// <summary>
  5. /// 固定写法
  6. /// </summary>
  7. /// <returns></returns>
  8. public IEnumerable<Type> GetRequiredInterfaces()
  9. {
  10. return Type.EmptyTypes;
  11. }
  12.  
  13. /// <summary>
  14. /// 可以记录调用时间, 参数, 函数名称
  15. /// </summary>
  16. /// <param name="input"></param>
  17. /// <param name="getNext"></param>
  18. /// <returns></returns>
  19. public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  20. {
  21. //getNext()(input, getNext); 关键点; 如果业务代码写在这句话之前, 那么就会先执行业务代码, 再执行配置文件中配置的实例代码; 反之同理
  22.  
  23. IMethodReturn methodReturn = getNext().Invoke(input, getNext);//执行后面的全部动作
  24. //原始方法执行后
  25.  
  26. Console.WriteLine("LogAfterBehavior");
  27. Console.WriteLine(input.MethodBase.Name);
  28. foreach (var item in input.Inputs)
  29. {
  30. Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
  31. //反射&序列化获取更多信息
  32. }
  33. Console.WriteLine("LogAfterBehavior" + methodReturn.ReturnValue);
  34. return methodReturn;
  35. }
  36.  
  37. /// <summary>
  38. /// 固定写法
  39. /// </summary>
  40. public bool WillExecute
  41. {
  42. get { return true; }
  43. }
  44. }

i)  ParameterCheckBehavior代码(在方法执行之前进行参数校验):

  1. /// <summary>
  2. /// 参数检查
  3. /// </summary>
  4. public class ParameterCheckBehavior : IInterceptionBehavior
  5. {
  6. public IEnumerable<Type> GetRequiredInterfaces()
  7. {
  8. return Type.EmptyTypes;
  9. }
  10.  
  11. public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  12. {
  13. Console.WriteLine("ParameterCheckBehavior");
  14. User user = input.Inputs[] as User;//可以不写死类型,反射+特性完成数据有效性监测
  15. if (user.Password.Length < )//可以过滤一下敏感词
  16. {
  17. //返回一个异常
  18. return input.CreateExceptionMethodReturn(new Exception("密码长度不能小于10位"));
  19. //注意只要抛出异常, 那么后面的都不会再执行了, 在这里也就是说后的 logafterbehavior是不会再执行了
  20. //throw new Exception("密码长度不能小于10位");
  21. }
  22. else
  23. {
  24. Console.WriteLine("参数检测无误");
  25. return getNext().Invoke(input, getNext);
  26. }
  27. }
  28.  
  29. public bool WillExecute
  30. {
  31. get { return true; }
  32. }
  33. }

j) ExceptionLoggingBehavior代码(异常处理):

  1. /// <summary>
  2. /// 异常处理
  3. /// </summary>
  4. public class ExceptionLoggingBehavior : IInterceptionBehavior
  5. {
  6. public IEnumerable<Type> GetRequiredInterfaces()
  7. {
  8. return Type.EmptyTypes;
  9. }
  10.  
  11. public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  12. {
  13. IMethodReturn methodReturn = getNext()(input, getNext);
  14.  
  15. Console.WriteLine("ExceptionLoggingBehavior");
  16. if (methodReturn.Exception == null) //检查methodReturn中是否有异常
  17. {
  18. Console.WriteLine("无异常");
  19. }
  20. else
  21. {
  22. Console.WriteLine($"异常:{methodReturn.Exception.Message}");
  23. }
  24. return methodReturn;
  25. }
  26.  
  27. public bool WillExecute
  28. {
  29. get { return true; }
  30. }
  31. }

k)  CachingBehavior代码(缓存处理):

  1. /// <summary>
  2. /// 缓存AOP
  3. /// </summary>
  4. public class CachingBehavior : IInterceptionBehavior
  5. {
  6. public IEnumerable<Type> GetRequiredInterfaces()
  7. {
  8. return Type.EmptyTypes;
  9. }
  10. /// <summary>
  11. /// 定义缓存字典
  12. /// </summary>
  13. private static Dictionary<string, object> CachingBehaviorDictionary = new Dictionary<string, object>();
  14.  
  15. public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  16. {
  17. Console.WriteLine("CachingBehavior");
  18. //把方法的名称和所有的参数列表全部标识为key , 然后放到字典里面;
  19. string key = $"{input.MethodBase.Name}_{Newtonsoft.Json.JsonConvert.SerializeObject(input.Inputs)}";
  20. //当方法名和参数都不变, 则表示有缓存
  21.  
  22. if (CachingBehaviorDictionary.ContainsKey(key))
  23. {
  24. return input.CreateMethodReturn(CachingBehaviorDictionary[key]);//直接返回 [短路器] 不再往下; 包括后面的Behavior也不会再执行了; 因为全部被直接短路了
  25. }
  26. else
  27. {
  28. //如果字典中没有, 则继续执行这里
  29. IMethodReturn result = getNext().Invoke(input, getNext);
  30. if (result.ReturnValue != null) //并将其加入到缓存列表中去; 当然缓存中放一个null没有任何意义, 所以这里判断一下
  31. {
  32. //不存在则, 加入缓存中
  33. CachingBehaviorDictionary.Add(key, result.ReturnValue);
  34. }
  35.  
  36. return result;
  37. }
  38.  
  39. }
  40.  
  41. public bool WillExecute
  42. {
  43. get { return true; }
  44. }
  45. }

l) UnityConfigAOP代码(调用):

  1. /// <summary>
  2. /// 使用EntLib\PIAB Unity 实现动态代理
  3. ///
  4. /// 1. 添加引用→右键→添加 NuGet包, 添加Unity的包引用
  5. /// 2. 这里添加的 Unity是5.8.13的版本, 而Unity.Interception是5.5.0; 因为最新的(5.5.5)不支持 .net 4.0
  6. /// 3. Unity是一个 Unity的容器; 而Unity.Interception当Unity做AOP时的一个扩展
  7. /// </summary>
  8. public class UnityConfigAOP
  9. {
  10. public static void Show()
  11. {
  12. User user = new User()
  13. {
  14. Name = "孙悟空",
  15. Password = ""
  16. };
  17.  
  18. {
  19. //1. 初始化UnityContainer容器
  20. IUnityContainer container = new UnityContainer();
  21. ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
  22.  
  23. //2. 开始读取配置文件
  24. fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");
  25. Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
  26. UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
  27.  
  28. //3. 使用配置文件中aopContainer节点下的所有配置信息来初始化container这个AOP容器
  29. configSection.Configure(container, "aopContainer"); //10. 这个aopContainer就是配置文件中的 <container name="aopContainer"> 这里这个容器的名称 ; 注意如果配置文件中的名字和这里的名字不一样就会报出以下错误:
  30. // The container named "aopContainer" is not defined in this configuration section.
  31.  
  32. // 在这里创建对象, 创建的规则就来自于配置文件的 <register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP"> 这一行; 如果是 IUserProcessor 类型, 就是使用UserProcessor 来创建实例
  33. IUserProcessor processor = container.Resolve<IUserProcessor>();
  34. processor.RegUser(user); //4. 注意, 当程序运行到这里的时候, 正常情况应该是去调用userProcessor的Reguser方法, 但是由于在配置文件中进行了配置, 所以它会去执行 LogBeforeBehavior 的 Invoke的方法(当aopContainer这容器中只有一个LogBeforeBehavior的时候, 如果多个, 则那个方法在前面则优先执行, 其它的上依次的调)
  35. User userNew1 = processor.GetUser(user); //调用GetUser的时候, 也会执行配置文件中对应配置了的方法
  36.  
  37. //演示缓存Behavior
  38. User userNew2 = processor.GetUser(user);
  39. }
  40. }
  41. }

20181122_C#中AOP_使用Unity实现AOP的更多相关文章

  1. 运用Unity实现AOP拦截器

    运用Unity实现AOP拦截器[结合异常记录实例] 本篇文章将通过Unity实现Aop异常记录功能:有关Unity依赖注入可以看前两篇文章: 1:运用Unity实现依赖注入[结合简单三层实例] 2:运 ...

  2. 运用Unity实现AOP拦截器[结合异常记录实例]

      本篇文章将通过Unity实现Aop异常记录功能:有关Unity依赖注入可以看前两篇文章: 1:运用Unity实现依赖注入[结合简单三层实例] 2:运用Unity实现依赖注入[有参构造注入] 另早期 ...

  3. Ioc 之 Unity的AOP功能

    前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...

  4. Xcode中如何集成Unity

    项目中需要集成unity,摸索了大半周,碰到了很多坑,终于搞定. 我的方法是,通过unity导出一个空的iOS项目,然后再新建一个Xcode项目,针对配置页面一一对应.直到配置完全一样,然后倒入相关资 ...

  5. 在.NET项目中使用PostSharp,实现AOP面向切面编程处理

    PostSharp是一种Aspect Oriented Programming 面向切面(或面向方面)的组件框架,适用在.NET开发中,本篇主要介绍Postsharp在.NET开发中的相关知识,以及一 ...

  6. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  7. unity中调试模型时unity崩溃问题

    这个问题是在我调试3D模型资源时出现的,每当在Scene场景中调试模型时unity崩溃,出现Unity Bug Reporter页面,反复出现这个问题,很烧脑 对于这个问题我表示很无语,但是经过不断查 ...

  8. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  9. [译]如何在ASP.NET Core中实现面向切面编程(AOP)

    原文地址:ASPECT ORIENTED PROGRAMMING USING PROXIES IN ASP.NET CORE 原文作者:ZANID HAYTAM 译文地址:如何在ASP.NET Cor ...

随机推荐

  1. Django中通过定时任务触发页面静态化的方式

    安装 pip install django-crontab 添加应用 INSTALLED_APPS = [ ... 'django_crontab', # 定时任务 ... ] 设置任务的定时时间 在 ...

  2. New Concept English there (5)

    25w/m Editors of newspapers and magazines often go to extremes to provide their readers with unimpor ...

  3. PostgreSQL copy命令使用记录

    上篇写到用pg_bulkload来导入数据,但是实际的环境是solaris,pg_bulkload还不支持,于是用copy的方式,下面附上脚本 -bash-4.1$ cat copy.sh #!/bi ...

  4. 从JDK源码角度看Boolean

    Java的Boolean类主要作用就是对基本类型boolean进行封装,提供了一些处理boolean类型的方法,比如String类型和boolean类型的转换. 主要实现源码如下: public fi ...

  5. HTTP 1.1 协议规范

    1. 内容协商 请求一个特殊编码的过程在 HTTP 1.1 规范中称为内容协商:

  6. js之验证码倒计时功能

    <!DOCTYPE html> <html > <head> <meta http-equiv="Content-Type" conten ...

  7. rancher下的kubernetes之一:构建标准化vmware镜像

    学习kubernetes的时候,我们需要在kubernetes环境下实战操作,然而kubernetes环境安装并不容易,现在通过rancher可以简化安装过程,咱们来实战rancher下的kubern ...

  8. 在IIS上搭建FTP站点

    操作环境 系统:win7 IIS版本:7.5 FTP传输工具:FlashXP 概述 本文介绍了如何在win7下利用IIS(默认已安装IIS和FTP功能)搭建FTP站点,FTP站点的常用配置. 快速搭建 ...

  9. CentOS跨网段访问

    centos6.2_64删除虚拟网卡 virbr0 卸载以下组件,然后重启系统 yum remove libvirt yum remove libvirt-python 来源:http://www.i ...

  10. HDU - 4339: Query(bitset暴力找下一个为1的)

    题意:给定A,B长度相同的字符串,Q次操作,修改操作位单个字符修改,查询操作为询问从某点开始有多少连续相同的字符. 思路:我们把不相同的设为1,相同的设为0,那么询问就是找下一个为1的为位置,可以用线 ...