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

上一篇文章我们通过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

运用Unity结合PolicyInjection实现拦截器的更多相关文章

  1. 运用Unity结合PolicyInjection实现拦截器[结合操作日志实例]

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

  2. C# unity 的 IInterceptionBehavior实现aop拦截器

    以前项目写过使用unity的 IInterceptionBehavior 实现aop拦截器,时间不多就忘了,项目找不到了,然后呢,写个简单的例子,用的收直接用就行了,简单实用,至于什么用,mvc的at ...

  3. 运用Unity实现AOP拦截器

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

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

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

  5. 我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器

    回到目录 AOP面向切面的编程,也称面向方面的编程,我更青睐于前面的叫法,将一个大系统切成多个独立的部分,而这个独立的部分又可以方便的插拔在其它领域的系统之中,这种编程的方式我们叫它面向切面,而这些独 ...

  6. 我心中的核心组件(可插拔的AOP)~第四回 异常拦截器

    回到目录 之前说过有关拦截器的文章,第二回  缓存拦截器,事实上,在那讲里说的最多是AOP和缓存组件,对于拦截的概念并没有详细的说明,这一讲,不说AOP,主要说一下拦截器,拦截器Interceptio ...

  7. (翻译) 使用Unity进行AOP对象拦截

    Unity 是一款知名的依赖注入容器( dependency injection container) ,其支持通过自定义扩展来扩充功能. 在Unity软件包内 默认包含了一个对象拦截(Interce ...

  8. 6. ModelDriven拦截器、Preparable 拦截器

    1. 问题 Struts2 的 Action 我们将它定义为一个控制器,但是由于在 Action 中也可以来编写一些业务逻辑,也有人会在 Action 输入业务逻辑层. 但是在企业开发中,我们一般会将 ...

  9. springmvc的拦截器

    什么是拦截器                                                         java里的拦截器是动态拦截action调用的对象.它提供了一种机制可以使 ...

随机推荐

  1. 详解JMeter函数和变量

    JMeter函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用.函数调用的语法如下: ${__functionName(var1,var2,var3)} 其中,__function ...

  2. IOS SDK相机的详细解释/画廊(默认+他们的高清摄像头接口)

    原版的blog,转载请注明出处 blog.csdn.net/hello_hwc 前言: 新NSURLSession的UploadTask的,结果写那个Demo的时候想要写成拍照上传.然后就想到先写一个 ...

  3. Spring + Spring MVC + Hibernate项目开发集成(注解)

    在自己从事的项目中都是使用xml配置的方式来进行的,随着项目的越来越大,会发现配置文件会相当的庞大,这个不利于项目的进行和后期的维护.于是考虑使用注解的方式来进行项目的开发,前些日子就抽空学习了一下. ...

  4. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  5. APP 半自适应 WEB页面

    特别赶,响应式纯自适应的,有空写了新的发. (在手机上看,页面上看一定乱) <!DOCTYPE html><html> <head> <meta http-e ...

  6. MySql状态查看方法 MySql如何查看连接数和状态?

    原文:MySql状态查看方法 MySql如何查看连接数和状态? 如果是root帐号,你能看到所有用户的当前连接.如果是其它普通帐号,只能看到自己占用的连接 怎么进入mysql命令行呢? mysql的安 ...

  7. 使用.net备份和还原数据库

    原文:使用.net备份和还原数据库 CSDN网友的提问http://community.csdn.net/Expert/TopicView3.asp?id=4929678C#实现SQLSERVER20 ...

  8. Grunt使用入门

    Grunt使用入门 (by vczero) 一.前言 项目中一直在使用Grunt,只是对Grunt的基本使用,却未系统的总结过.为什么要构建工具?一句话:自动化.对于需要反复重复的任务,例如压缩(mi ...

  9. 前端JS开发框架

    前端JS开发框架-DHTMLX 发框架-DHTMLX   一:介绍 dhtmlxSuite是一个JavaScript库,提供了一套完整的Ajax -驱动UI组件.我们能够使用dhtmlxSuite构建 ...

  10. SSMS2008插件开发(2)--Microsoft Visual Studio 2008插件开发介绍

    原文:SSMS2008插件开发(2)--Microsoft Visual Studio 2008插件开发介绍 由于开发SSMS2008插件是通过VS2008进行的,有必要先介绍一下VS2008的插件开 ...