使用Autofac做类的增强

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

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

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

都有哪些办法呢?本系列尝试以下几种方法,来实现对原有业务类的无侵入增强:


原有业务类

业务模型

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);
        }
    }
}

使用Autofac做类的增强

我们将使用第三方的Autofac来对原有的类做业务增强,首先使用NuGet安装。
使用Autofac做类的增强
业务增强实现

using Castle.DynamicProxy;
using System;

namespace testAopByAutofac
{
    public class UserProcessorLog : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            User user = invocation.Arguments[0] as User;

            before(user);
            invocation.Proceed();
            after(user);
        }

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

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

上层调用

using Autofac;
using Autofac.Extras.DynamicProxy2;
using System;

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

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

        private static void RegisterAndLog()
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder
                .RegisterType<UserProcessor>()
                .As<IUserProcessor>()
                .EnableInterfaceInterceptors()
                .InterceptedBy(typeof(UserProcessorLog));
            builder.RegisterType<UserProcessorLog>();

            var container =  builder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                IUserProcessor processor = (IUserProcessor)scope.Resolve<IUserProcessor>();

                processor.RegisterUser(user);
            }
        }
    }
}

对比一下扩展前后的业务展现

使用Autofac做类的增强

这里我们使用代码的方式给原有的业务类配置了拦截器(AOP依赖注入),也可以使用特性的方式。
使用特性加强业务类

using Autofac.Extras.DynamicProxy2;
using System;

namespace testAopByAutofac
{
    [Intercept(typeof(UserProcessorLog))]
    public class UserProcessor : IUserProcessor
    {
        public void RegisterUser(User user)
        {
            if (user == null)
            {
                return;
            }

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

上层调用

using Autofac;
using Autofac.Extras.DynamicProxy2;
using System;

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

        static void Main(string[] args)
        {
            Register();
            Console.WriteLine("--------------");
            RegisterAndLog();

            Console.ReadKey();
        }

        private static void RegisterAndLog()
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder
                .RegisterType<UserProcessor>()
                .As<IUserProcessor>()
                .EnableInterfaceInterceptors();
                //.InterceptedBy(typeof(UserProcessorLog));//可使用特性代替
            builder.RegisterType<UserProcessorLog>();

            var container =  builder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                IUserProcessor processor = (IUserProcessor)scope.Resolve<IUserProcessor>();

                processor.RegisterUser(user);
            }
        }

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

上面我们使用的是接口注入(EnableInterfaceInterceptors),也可以使用类注入(EnableClassInterceptors),此时原有业务类的方法需要显示声明为vitual虚方法。在上层调用的时候,我们就可以直接注册和解析一个类,不依赖于接口了。

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


下一篇:使用Castle做类的增强