上一篇文章我们通过Unity自身Unity.InterceptionExtension.IInterceptionBehavior实现一个有系统关异常日志记录;解决代码中到处充满的异常记录的代码;

本文则是通过Unity.InterceptionExtension.ICallHandler实现一个操作日志记录功能;在相应操作方法上通过特性Attribute把操作日志进行统一处理;若想了解Unity依赖注入及AOP功能可以查看先前其它文章;

1:首先我们在公共助手层Command层新建OperateLogCallHandler类及OperateLogAttribute类

其中类OperateLogCallHandler继承自ICallHandler,并且我们定义的属性(MessageInfo,ShowThrow),此处我们只是简单的显示出操作内容信息,若实际项目可以对下面进行简单修改便可,比如写入日志、数据库等;代码result.Exception == null则表示执行代码没有出现异常才逻辑写入

using Microsoft.Practices.Unity.InterceptionExtension;namespace Command
{public class OperateLogCallHandler:ICallHandler{public string _messageInfo { set; get; }public bool _showThrow { get; set; }private int _order = 0;public OperateLogCallHandler(string MessageInfo, bool ShowThrow){this._messageInfo = MessageInfo;this._showThrow = ShowThrow;}public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext){if (input == null) throw new ArgumentNullException("input");if (getNext == null) throw new ArgumentNullException("getNext");var result = getNext()(input, getNext);if (result.Exception == null){//进行逻辑代码编写  比如把操作内容写入数据库Console.WriteLine("操作的内容为:" + _messageInfo);}if (_showThrow) result.Exception = null;return result;}public int Order{get{return _order;}set{_order = value;}}}
}

而类OperateLogAttribute则继承自HandlerAttribute,并且我们设定此特性只能作用于方法上;

using Microsoft.Practices.Unity.InterceptionExtension;
namespace Command
{[AttributeUsage(AttributeTargets.Method)]public class OperateLogAttribute : HandlerAttribute{public string messageInfo { get; set; }public bool showThrow { get; set; }public OperateLogAttribute(){}public OperateLogAttribute(string MessagInfo, bool ShowThrow){this.messageInfo = MessagInfo;this.showThrow = ShowThrow;}public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container){OperateLogCallHandler handler = new OperateLogCallHandler(this.messageInfo, this.showThrow);handler.Order = this.Order;return handler;}}
}

2:公共层里还有一个助手类UnityContainerHelp,用于读取加载配置

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity.InterceptionExtension.Configuration;
using System.Configuration;
using System.Reflection;namespace Command
{public class UnityContainerHelp{private IUnityContainer container;public UnityContainerHelp(){container = new UnityContainer();UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");container.LoadConfiguration(section, "FirstClass");}public T GetServer<T>(){return container.Resolve<T>();}/// <summary>/// /// </summary>/// <typeparam name="T"></typeparam>/// <param name="ConfigName">配置文件中指定的文字</param>/// <returns></returns>public T GetServer<T>(string ConfigName){return container.Resolve<T>(ConfigName);}/// <summary>/// 返回构结函数带参数/// </summary>/// <typeparam name="T">依赖对象</typeparam>/// <param name="parameterList">参数集合(参数名,参数值)</param>/// <returns></returns>public T GetServer<T>(Dictionary<string, object> parameterList){var list = new ParameterOverrides();foreach (KeyValuePair<string, object> item in parameterList){list.Add(item.Key, item.Value);}return container.Resolve<T>(list);}/// <summary>/// 返回构结函数带参数/// </summary>/// <typeparam name="T">依赖对象</typeparam>/// <param name="ConfigName">配置文件中指定的文字(没写会报异常)</param>/// <param name="parameterList">参数集合(参数名,参数值)</param>/// <returns></returns>public T GetServer<T>(string ConfigName,Dictionary<string,object> parameterList){var list = new ParameterOverrides();foreach (KeyValuePair<string, object> item in parameterList) {list.Add(item.Key, item.Value);}return container.Resolve<T>(ConfigName,list);}}
}

3:接着在接口层相应的方法上使用我们刚才创建的特性,注意必需引用Microsoft.Practices.Unity.InterceptionExtension.dll;若则特性将会找不到

using Microsoft.Practices.Unity.InterceptionExtension;
using Command;
namespace IAopBLL
{public interface IUser{[OperateLog(Order = 10, messageInfo = "增加用户", showThrow = true)]void CreateUser();[OperateLog(Order = 10, messageInfo = "编辑用户", showThrow = true)]void UpdateUser();}
}

4:在BLL层里实现上面的接口,在方法里面我们就不进行任何操作

using IAopDAL;
using IAopBLL;
using Command;
namespace AopBLL
{public class UserBLL:IUser{public void CreateUser(){//逻辑代码,此处为了简单就省略不写
        }public void UpdateUser(){//逻辑代码,此处为了简单就省略不写
        }}
}

5:下面为主程序调用代码,这里我们简单运用到Unity依赖注入代码

using IAopBLL;
using Command;
namespace AopUnity
{class Program{static void Main(string[] args){Console.WriteLine("-----------------------------------");IUser bllProperty = new UnityContainerHelp().GetServer<IUser>("UserBllAop");bllProperty.CreateUser();bllProperty.UpdateUser();Console.WriteLine("-----------------------------------");}}
}

配置文件的内容如下:其中有两个比较要注意的地方(a处 name="UserBllAop" 主程序里有调用 b处 <policyInjection/>拦载器才会起作用)

<?xml version="1.0"?>
<configuration><configSections><section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/></configSections><unity xmlns="http://schemas.microsoft.com/practces/2010/unity"><sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/><container name="FirstClass"><extension type="Interception"/><register type="IAopBLL.IUser,IAopBLL" mapTo="AopBLL.UserBLL,AopBLL" name="UserBllAop"><interceptor type="InterfaceInterceptor" /><policyInjection/></register></container></unity>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

6:运行效果:

从图中我们不难发现Aop已起到作用,在操作没有异常的情况下我们成功获得操作内容;当然其它Aop比如权限判断,事务处理等都可以用此种办法;

运用Unity结合PolicyInjection实现拦截器[结合操作日志实例]相关推荐

  1. 基于springMVC拦截器实现操作日志统计

    2019独角兽企业重金招聘Python工程师标准>>> 1.spring配置文件配置. <!-- 拦截器 --><mvc:interceptors><! ...

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

    运用Unity实现AOP拦截器[结合异常记录实例] 参考文章: (1)运用Unity实现AOP拦截器[结合异常记录实例] (2)https://www.cnblogs.com/wujy/p/33233 ...

  3. mysql 数据拦截器_拦截器中操作数据库

    做了个小项目,当初设计的是只有一个模块的用户行为被记录,其他不用记录,昨天突然说,要用户在整个系统的行为都要被记录. 很懵逼,如果把用户行为的记录放在各个模块,可以很精确的记录,但是各个模块都要有更改 ...

  4. spring拦截器使用及拦截器中获取Controller实例

    1,首先是配置,在spring-mvc的配置文件中加上 <!-- 拦截器 --> <mvc:interceptors><bean class="cn.hydom ...

  5. java创建请求拦截器_80.简单Retrofit+RxJava+日志拦截器结合使用

    1.需要使用到的依赖如下(Retrofit\RxJava\RecyclerView\日志拦截器) //only Retrofit(只用Retrofit联网) implementation 'io.re ...

  6. WebServices中使用cxf开发日志拦截器以及自定义拦截器

    首先下载一个cxf实例,里面包含cxf的jar包.我下的是apache-cxf-2.5.9 1.为什么要设置拦截器? 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截 ...

  7. struts2(三) 输入校验和拦截器

    前面知道了struts2的架构图和struts2的自动封装表单参数和数据类型自动转换,今天来学struts2的第三第四个东西,输入校验和拦截器, --WH 一.输入校验 在以前我们写一个登录页面时,并 ...

  8. Struts2 源码分析——拦截器的机制

    本章简言 上一章讲到关于action代理类的工作.即是如何去找对应的action配置信息,并执行action类的实例.而这一章笔者将讲到在执行action需要用到的拦截器.为什么要讲拦截器呢?可以这样 ...

  9. 过滤器跟拦截器的区别

    一.拦截器与过滤器的区别总结 1. 原理不同:拦截器是基于java的反射机制的,而过滤器是基于函数回调. 2. 依赖容器:拦截器不依赖与servlet容器,过滤器依赖与servlet容器. 3. 作用 ...

  10. Struts2拦截器实现异常处理机制

    http://bbs.itcast.cn/thread-10364-1-1.html Struts2拦截器实现异常处理机制   在j2ee项目中,系统内部难免会出现一些异常,如果把异常放任不管直接打印 ...

最新文章

  1. 线程安全(中)--彻底搞懂synchronized(从偏向锁到重量级锁)
  2. FPGA笔试题解析(二)
  3. generator自动生成mybatis配置和类信息
  4. linux: convmv =-======pkgs.org
  5. 【Java 并发编程】线程池机制 ( 测试线程开销 | 启动线程分析 | 用户态 | 内核态 | 用户线程 | 内核线程 | 轻量级进程 )
  6. 小姐姐笔记:我是如何学习简单源码拓展视野的
  7. 深度相机(三)--三种方案对比
  8. SQL查询效率注意事项 2011.12.27
  9. if语句使用说明(Java)
  10. 2020软考论文想要拿高分,要避开这些坑!
  11. (三)【机器人路径规划】Astar算法
  12. 乞讨网站,要饭网,个人要饭网,在线要饭网站;含socket 通信;双端支付源码 ;源码
  13. xcode 免cleanup build
  14. python代码雨_教你用200行Python代码“换脸”
  15. Linux怎么把硬盘ex2改为ex4,linux磁盘管理和文件系统创建
  16. 【GAMES101 课程小结】:Lecture 13 Ray Tracing
  17. Matlab——常用函数的用法总结(部分直接摘自mathwork,持续更新)
  18. 坚持#第370天~优酷视频使用PC端的优酷就可以下载下来了
  19. 最新手机号验证正则表达式(电信、移动、广电号段)
  20. RDkit |基于RDkit计算PBF(Plane of Best Fit)描述符数值

热门文章

  1. 10 步让你成为更优秀的Coder
  2. GARFIELD@02-21-2005
  3. 简述prototype, _proto_, constructor三者的关系
  4. Redis简介、安装、配置、启用学习笔记
  5. 多线程开发必须知道的概念
  6. Zend_Db_Table-insert ()和zend_db_adapter::insert方法返回值不同
  7. nginx负载均衡的方法
  8. AI 深度关键短语生成
  9. 076 hashlib模块和hmac模块
  10. ubuntu安装python