使用Unity做类的增强(续)

我们已经实现了用户注册功能,现在想增加日志记录功能。具体来讲就是在用户注册前后,分别输出一条日志。我们当然可以修改原有的业务代码。

现在换个角度来问两个问题:
1. 团队开发中,我们很可能根本拿不到源代码,那又怎么去增加这个功能呢?
2. 这次需求是增加日志,以后再增加其他需求(比如异常处理),是不是仍然要改业务类呢?

总结一下:
我们要在不修改原有类业务代码的前提下,去做类的增强。我们的设计要符合面向对象的原则:对扩展开放,对修改封闭

都有哪些办法呢?我们尝试以下几种方法:


上次我们使用unity实现了log日志的增强,这次我们来实现异常处理、权限验证两个需求;并且不使用拦截器的方式,而是使用Attribute给原有业务类来打标签的方式来达到业务增强的目的。

原有业务类

业务模型

namespace testAopByDecorator
{
    public class User
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }
}

接口设计

namespace testAopByDecorator
{
    public interface IUserProcessor
    {
        void RegisterUser(User user);
    }
}

业务实现

using System;

namespace testAopByDecorator
{
    public class UserProcessor : IUserProcessor
    {
        public void RegisterUser(User user)
        {
            if (user == null)
            {
                return;
            }

            Console.WriteLine(string.Format("注册了一个用户{0}:{1}", user.Id, user.Name));
        }
    }
}

上层调用

using System;

namespace testAopByDecorator
{
    class Program
    {
        private static User user = new User { Id = 1, Name = "滇红" };

        static void Main(string[] args)
        {
            Register();
            Console.ReadKey();
        }

        private static void Register()
        {
            IUserProcessor processor = new UserProcessor();
            processor.RegisterUser(user);
        }
    }
}

使用Unity做类的增强

我们将使用第三方的Unity来对原有的类做业务增强,首先使用NuGet安装。
使用Unity做类的增强(续)

日志Attribute类

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

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

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            User user = input.Inputs[0] as User;

            before(user);
            InvokeHandlerDelegate delegateMethod = getNext();
            IMethodReturn returnMessag = delegateMethod(input, getNext);
            after(user);

            return returnMessag;
        }

        private void after(User user)
        {
            Console.WriteLine("日志结束:" + user.Name);
        }

        private void before(User user)
        {
            Console.WriteLine("日志开始:" + user.Name);
        }
    }
}

异常Attribute类

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

namespace testAopByUnityAttribute
{
    public class ExceptionHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            ICallHandler handler = new UserProcessorException { Order = this.Order };
            return handler;
        }
    }
    public class UserProcessorException : ICallHandler
    {
        public int Order { get; set; }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            User user = input.Inputs[0] as User;

            before(user);
            InvokeHandlerDelegate delegateMethod = getNext();
            IMethodReturn returnMessag = delegateMethod(input, getNext);

            if (returnMessag.Exception != null)
            {
                Console.WriteLine("捕获了异常:" + returnMessag.Exception.Message);
                returnMessag.Exception = null; //结束异常栈
            }

            after(user);

            return returnMessag;
        }

        private void after(User user)
        {
            Console.WriteLine("异常捕获后:" + user.Name);
        }

        private void before(User user)
        {
            Console.WriteLine("异常捕获前:" + user.Name);
        }
    }
}

权限Attribute类

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

namespace testAopByUnityAttribute
{
    public class AuthorizeHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            ICallHandler handler = new UserProcessorAuthorize { Order = this.Order };
            return handler;
        }
    }
    public class UserProcessorAuthorize : ICallHandler
    {
        public int Order { get; set; }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            User user = input.Inputs[0] as User;

            Console.WriteLine("权限验证中......");

            if(user == null || !user.Name.Equals("admin"))
            {
                //抛出异常
                return input.CreateExceptionMethodReturn(new Exception("没有这个用户!"));
            }

            before(user);
            InvokeHandlerDelegate delegateMethod = getNext();
            IMethodReturn returnMessage = delegateMethod(input, getNext);
            after(user);

            return returnMessage;
        }

        private void after(User user)
        {
            Console.WriteLine("用户注册后:" + user.Name);
        }

        private void before(User user)
        {
            Console.WriteLine("用户注册前:" + user.Name);
        }
    }
}

给业务接口打标签,原有业务类会自动继承

namespace testAopByUnityAttribute
{
    //使用Order来决定特性的执行时序
    [ExceptionHandler(Order =1)]
    [LogHandler(Order = 2)]
    [AuthorizeHandler(Order =3)]

    public interface IUserProcessor
    {
        void RegisterUser(User user);
    }
}

上层调用

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

namespace testAopByUnityAttribute
{
    class Program
    {
        private static User user = new User { Id = 1, Name = "admin" };

        static void Main(string[] args)
        {
            RegisterAndLog();
            Console.ReadKey();
        }

        private static void RegisterAndLog()
        {
            //创建容器
            IUnityContainer container = new UnityContainer();

            //注册服务
            container.RegisterType<IUserProcessor, UserProcessor>();
            //扩展拦截器
            container.AddNewExtension<Interception>().Configure<Interception>()
                .SetInterceptorFor<IUserProcessor>(new InterfaceInterceptor());

            //调用服务
            IUserProcessor processor = container.Resolve<IUserProcessor>();

            try
            {
                processor.RegisterUser(user);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

对比一下扩展前后的业务展现
使用Unity做类的增强(续)

上一篇:VB源码之友2006 测试版发布,欢迎大家下载测试


下一篇:Linux 下查看某一个程序所使用的内存方法