上一篇文章我们通过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 = ; 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 = , messageInfo = "增加用户", showThrow = true)]
void CreateUser(); [OperateLog(Order = , 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. 运用Unity结合PolicyInjection实现拦截器

    运用Unity结合PolicyInjection实现拦截器[结合操作日志实例] 上一篇文章我们通过Unity自身Unity.InterceptionExtension.IInterceptionBeh ...

  2. Struts2拦截器记录系统操作日志

    前言 最近开发了一个项目,由于项目在整个开发过程中处于赶时间状态(每个项目都差不多如此)所以项目在收尾阶段发现缺少记录系统日志功能,以前系统都是直接写在每个模块的代码中,然后存入表单,在页面可以查看部 ...

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

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

  4. MVC拦截器记录操作用户日志

    主要是用于记录用户操作动态, public class OperationAttribute:ActionFilterAttribute { /// <summary> /// 方法名称 ...

  5. IntelliJ IDEA 2017版 spring-boot 拦截器的操作三种方式

    一.注解方式 @WebServlet(urlPatterns = "/myServlet") public class MyServlet extends HttpServlet ...

  6. SSH基于Hibernate eventListener 事件侦听器的操作日志自动保存到数据库

    在spring xml配置文件中添加配置,包含:model.listener 在model中增加需要写入数据库对应表的model 在auditLog.xml配置文件中配置自己项目中,需要进行日志记录的 ...

  7. 运用Unity实现AOP拦截器

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

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

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

  9. Hibernate拦截器(Interceptor)与事件监听器(Listener)

    拦截器(Intercept):与Struts2的拦截器机制基本一样,都是一个操作穿过一层层拦截器,每穿过一个拦截器就会触发相应拦截器的事件做预处理或善后处理. 监听器(Listener):其实功能与拦 ...

随机推荐

  1. PHP超全局变量数组

    以下8个变量都是数组变量,又称为“预定义变量” 1.$_GET : 把数据通过地址栏传递到服务器,这是方式必须是$_GET方式传递: 2.$_POST : 通过表单发送的数据必须是POST方式: 3. ...

  2. 主元素 II

    主元素 II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时间复 ...

  3. PMP CMM

    CMM和PMP是两个不同的概念域,是用来解决不同问题的.我们所说的CMM,准确的说应该是叫做能力成熟度模型,北京猴子说的软件能力成熟度模型实际上应该称为SW-CMM,是CMM的一个子集.PMP可以看做 ...

  4. 【树形dp】TELE

    [POJ1155]TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5376   Accepted: 2973 Des ...

  5. 【多重背包】CDOJ1691 这是一道比CCCC简单题经典的中档题

    #include<cstdio> #include<algorithm> using namespace std; int n,V,w[110],c[110],a[110],f ...

  6. python基础之生成器,三元表达式,列表生产式

    生成器 1.什么是生成器? 在函数内但凡出现yield关键字,再调用函数就不会执行函数体代码,会返回一个值,该值称为生成器. 生成器的本质就是迭代器. 2.为什么要用生成器? 生成器是一种自定义迭代器 ...

  7. Excel | 如何用Excel实现证件照底色调换

    这段时间因为一些事情需要用到证件照这个东西,大家应该都清楚,不管是简历还是各种考试上面,都需要贴上一张规规矩矩的证件照片或是上传电子照片. 通常,我们到照相馆照证件照的时候,无外乎红底.蓝底以及白底这 ...

  8. mysql锁机制整理

    Auth: jinDate: 20140506 主要参考整理资料MYSQL性能调优与架构设计-第七章 MYSQL锁定机制http://www.cnblogs.com/ggjucheng/archive ...

  9. Eclipse运行Maven的SpringMVC项目Run on Server时出现错误:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener的问题解决

    错误: 严重: Error configuring application listener of class org.springframework.web.context.ContextLoade ...

  10. 如何获取php错误

    今天把项目放在测试服务器,但是出现一个问题,用的TP5框架,我把入口文件放在了根目录,访问的时候报错了,框架引导文件引入不了,也不报错,就是说访问不了. 所以就用了一段代码把错误获取出来了,代码如下: ...