.Net AOP (四)EnterpriseLibary 实现方法

.Net AOP (四)EnterpriseLibary 实现方法

首先添加EnterpriseLibary的引用

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

using Microsoft.Practices.Unity.InterceptionExtension;

 public class UserHandler:ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            User user = input.Inputs[0] as User;
            if (user.PassWord.Length < 10)
            {
                return input.CreateExceptionMethodReturn(new UserException("密码长度不能小于10位"));
            }
            Console.WriteLine("参数检测无误");
            return getNext()(input, getNext);
        }
    }

    public class LogHandler:ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            User user = input.Inputs[0] as User;
            Log log = new Log() { Message = string.Format("RegUser:Username:{0},Password:{1}", user.Name, user.PassWord), Ctime = DateTime.Now };
            Console.WriteLine("日志已记录,Message:{0},Ctime:{1}",log.Message,log.Ctime);
            var messagereturn  = getNext()(input, getNext); 
            return messagereturn;
        }
    }

定义对应的HandlerAttribute

using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity;

    public class UserHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            ICallHandler handler = new UserHandler(){Order=this.Order};
            return handler;
        }
    }

    public  class LogHandlerAttribute:HandlerAttribute
    {
        public int Order { get; set; }
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new LogHandler() { Order = this.Order };
        }
    }

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

[LogHandlerAttribute(Order=2)]
    [UserHandlerAttribute(Order=1)]
    public interface IUserProcessor
    {
         void RegUser(User user);
    }

    public class UserProcessor : MarshalByRefObject,IUserProcessor
    {
        public  void RegUser(User user)
        {
            Console.WriteLine("用户已注册。");
        }
    }

测试

using Microsoft.Practices.EnterpriseLibrary.PolicyInjection; 
   
    public class Client
    {
        public static void Run()
        {
            try
            {
                User user = new User() { Name = "lee", PassWord = "123123123123" };
                UserProcessor userprocessor = PolicyInjection.Create<UserProcessor>();
                userprocessor.RegUser(user);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值