我们已经实现了用户注册功能,现在想增加日志记录功能。具体来讲就是在用户注册前后,分别输出一条日志。我们当然可以修改原有的业务代码。
现在换个角度来问两个问题:
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);
}
}
}
使用Unity做类的增强
我们将使用第三方的Unity来对原有的类做业务增强,首先使用NuGet安装。
业务增强实现
using System;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
namespace testAopByUnityBehavior
{
public class UserProcessorLogBehavior : IInterceptionBehavior
{
public bool WillExecute
{
get
{
return true;
}
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
User user = input.Inputs[0] as User;
before(user);
InvokeInterceptionBehaviorDelegate 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);
}
}
}
上层调用
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;
namespace testAopByUnityBehavior
{
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.AddNewExtension<Interception>();
container.RegisterType<IUserProcessor, UserProcessor>(
new InjectionConstructor(), //构造器拦截
new Interceptor<InterfaceInterceptor>(),//注册拦截器
new InterceptionBehavior<UserProcessorLogBehavior>() //注册行为拦截
);
User user = new User { Id = 1, Name = "李丹丹" };
//从容器拿到服务(以为加了拦截器,这里生成的是动态对象)
IUserProcessor processor = container.Resolve<IUserProcessor>();
processor.RegisterUser(user);
}
}
}
对比一下扩展前后的业务展现
这里我们使用代码的方式给原有的业务类配置了拦截器(AOP依赖注入),也可以使用配置文件的方式,这样会更加的灵活。
配置文件:unity.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="singleton"
type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" />
<container>
<extension type="Interception" />
<register type="IInterceptionBehavior"
mapTo="testAopByUnityConfig.UserProcessorLogBehavior,testAopByUnityConfig"
name="UserProcessorLogBehavior">
</register>
<register type="testAopByUnityConfig.IUserProcessor,testAopByUnityConfig"
mapTo="testAopByUnityConfig.UserProcessor,testAopByUnityConfig">
<interceptionBehavior name="UserProcessorLogBehavior"/>
<interceptor type="InterfaceInterceptor"/>
</register>
</container>
</unity>
</configuration>
上层调用
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity.InterceptionExtension.Configuration;
using System;
using System.Configuration;
namespace testAopByUnityConfig
{
class Program
{
private static User user = new User { Id = 1, Name = "admin" };
static void Main(string[] args)
{
RegisterAndLog();
Console.ReadKey();
}
private static void RegisterAndLog()
{
var map = new ExeConfigurationFileMap { ExeConfigFilename = "unity.config" };//使用此配置文件
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection("unity");//读取配置文件节点
var container = new UnityContainer();
container.LoadConfiguration(section);
//从容器拿到服务(因为加了拦截器,这里生成的是动态对象)
IUserProcessor processor = container.Resolve<IUserProcessor>();
processor.RegisterUser(user);
}
}
}