.Net AOP (四)EnterpriseLibary 实现方法

来源:互联网 发布:c语言函数声明的标准 编辑:程序博客网 时间:2024/06/08 02:31

.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.     }  


0 0
原创粉丝点击