.Net AOP (四)EnterpriseLibary 实现方法

本文介绍如何利用.NET Enterprise Library实现面向切面编程(AOP),包括自定义CallHandler进行参数验证与日志记录,以及通过属性注解实现特定方法的拦截。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.Net AOP (四)EnterpriseLibary 实现方法

首先添加EnterpriseLibary的引用

自定义CallHandler,这里定义两个CallHandler分别用于参数检查和日志记录。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using Microsoft.Practices.Unity.InterceptionExtension;  
  2.   
  3.  public class UserHandler:ICallHandler  
  4.     {  
  5.         public int Order { get; set; }  
  6.         public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)  
  7.         {  
  8.             User user = input.Inputs[0] as User;  
  9.             if (user.PassWord.Length < 10)  
  10.             {  
  11.                 return input.CreateExceptionMethodReturn(new UserException("密码长度不能小于10位"));  
  12.             }  
  13.             Console.WriteLine("参数检测无误");  
  14.             return getNext()(input, getNext);  
  15.         }  
  16.     }  
  17.   
  18.     public class LogHandler:ICallHandler  
  19.     {  
  20.         public int Order { get; set; }  
  21.         public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)  
  22.         {  
  23.             User user = input.Inputs[0] as User;  
  24.             Log log = new Log() { Message = string.Format("RegUser:Username:{0},Password:{1}", user.Name, user.PassWord), Ctime = DateTime.Now };  
  25.             Console.WriteLine("日志已记录,Message:{0},Ctime:{1}",log.Message,log.Ctime);  
  26.             var messagereturn  = getNext()(input, getNext);   
  27.             return messagereturn;  
  28.         }  
  29.     }  

定义对应的HandlerAttribute

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using Microsoft.Practices.Unity.InterceptionExtension;  
  2. using Microsoft.Practices.Unity;  
  3.   
  4.     public class UserHandlerAttribute : HandlerAttribute  
  5.     {  
  6.         public override ICallHandler CreateHandler(IUnityContainer container)  
  7.         {  
  8.             ICallHandler handler = new UserHandler(){Order=this.Order};  
  9.             return handler;  
  10.         }  
  11.     }  
  12.   
  13.     public  class LogHandlerAttribute:HandlerAttribute  
  14.     {  
  15.         public int Order { get; set; }  
  16.         public override ICallHandler CreateHandler(IUnityContainer container)  
  17.         {  
  18.             return new LogHandler() { Order = this.Order };  
  19.         }  
  20.     }  

用户注册接口和实现,这里通过为接口添加attribute的方式实现。order值表示执行顺序,值小的先执行。

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [LogHandlerAttribute(Order=2)]  
  2.     [UserHandlerAttribute(Order=1)]  
  3.     public interface IUserProcessor  
  4.     {  
  5.          void RegUser(User user);  
  6.     }  
  7.   
  8.     public class UserProcessor : MarshalByRefObject,IUserProcessor  
  9.     {  
  10.         public  void RegUser(User user)  
  11.         {  
  12.             Console.WriteLine("用户已注册。");  
  13.         }  
  14.     }  

测试

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;   
  2.      
  3.     public class Client  
  4.     {  
  5.         public static void Run()  
  6.         {  
  7.             try  
  8.             {  
  9.                 User user = new User() { Name = "lee"PassWord = "123123123123" };  
  10.                 UserProcessor userprocessor = PolicyInjection.Create<UserProcessor>();  
  11.                 userprocessor.RegUser(user);  
  12.             }  
  13.             catch(Exception ex)  
  14.             {  
  15.                 throw ex;  
  16.             }  
  17.         }  
  18.     }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值