理论概述
/// AOP:面向切面编程 编程思想
/// OOP:一切皆对象,对象交互组成功能,功能叠加组成模块,模块叠加组成系统
/// 类--砖头 系统--房子
/// 类--细胞 系统--人
/// 面向对象是非常适合做大型系统
/// 应对需求变化扩展的时候,比较困难;
/// 面向对象是静态的:任何需求的细微变化,都可能导致比较大的影响
///
/// 设计模式:设计出灵活 可扩展 可重用 的架构 可以解决部分问题
/// 都在折腾抽象/类
/// 永远无法解决类的内部变化
///
/// AOP:面向切面编程 编程思想
/// 就是解决类的内部变化问题
/// 能做到让开发者动态的修改一个静态的面向对象模型,
/// 在不破坏封装的前提下,去增加各种功能:非业务逻辑,是一些公共逻辑
/// 是对OOP的有效补充
/// 有了AOP之后,OOP也变得简单了
装饰器以及代理模式实现方法
/// <summary>
/// 装饰器模式实现静态代理
/// AOP 在方法前后增加自定义的方法
///
/// 简单的proxy静态AOP
/// </summary>
public class DecoratorAOP
{
public static void Show()
{
User user = new User()
{
Name = "老六",
Password = "123123123123"
};
IUserProcessor processor = new UserProcessor();
processor.RegUser(user);//没有使用AOP
Console.WriteLine("***************");
processor = new UserProcessorDecorator(processor);
processor.RegUser(user);//使用了AOP
}
public interface IUserProcessor
{
void RegUser(User user);
}
public class UserProcessor : IUserProcessor
{
public void RegUser(User user)
{
Console.WriteLine("用户已注册。Name:{0},PassWord:{1}", user.Name, user.Password);
}
}
/// <summary>
/// 装饰器的模式去提供一个AOP功能
/// </summary>
public class UserProcessorDecorator : IUserProcessor
{
private IUserProcessor UserProcessor { get; set; }
public UserProcessorDecorator(IUserProcessor userprocessor)
{
UserProcessor = userprocessor;
}
public void RegUser(User user)
{
BeforeProceed(user);
this.UserProcessor.RegUser(user);
AfterProceed(user);
}
/// <summary>
/// 业务逻辑之前
/// </summary>
/// <param name="user"></param>
public void BeforeProceed(User user)
{
Console.WriteLine("方法执行前");
}
/// <summary>
/// 业务逻辑之后
/// </summary>
/// <param name="user"></param>
public void AfterProceed(User user)
{
Console.WriteLine("方法执行后");
}
}
代理模式以及两者的区别参照 https://www.cnblogs.com/LJP-JumpAndFly/p/12165369.html
代理模式分为静态代理和动态代理,静态代码量会增大很多,动态更符合AOP的思想
下面是代理模式实现aop动态
/// <summary>
/// 使用.Net Remoting/RealProxy 实现动态代理
/// 局限在业务类必须是继承自MarshalByRefObject类型
/// </summary>
public class RealProxyAOP
{
public static void Show()
{
User user = new User()
{
Name = "Eleven",
Password = "123456"
};
UserProcessor processor = new UserProcessor();
processor.RegUser(user);
Console.WriteLine("*********************");
UserProcessor userProcessor = TransparentProxy.Create<UserProcessor>();
userProcessor.RegUser(user);
}
/// <summary>
/// 真实代理
/// </summary>
/// <typeparam name="T"></typeparam>
public class MyRealProxy<T> : RealProxy
{
private T tTarget;
public MyRealProxy(T target)
: base(typeof(T))
{
this.tTarget = target;
}
public override IMessage Invoke(IMessage msg)
{
BeforeProceede(msg);
IMethodCallMessage callMessage = (IMethodCallMessage)msg;
object returnValue = callMessage.MethodBase.Invoke(this.tTarget, callMessage.Args);
AfterProceede(msg);
return new ReturnMessage(returnValue, new object[0], 0, null, callMessage);
}
public void BeforeProceede(IMessage msg)
{
Console.WriteLine("方法执行前可以加入的逻辑");
}
public void AfterProceede(IMessage msg)
{
Console.WriteLine("方法执行后可以加入的逻辑");
}
}
/// <summary>
/// 透明代理
/// </summary>
public static class TransparentProxy
{
public static T Create<T>()
{
T instance = Activator.CreateInstance<T>();
MyRealProxy<T> realProxy = new MyRealProxy<T>(instance);
T transparentProxy = (T)realProxy.GetTransparentProxy();
return transparentProxy;
}
}
public interface IUserProcessor
{
void RegUser(User user);
}
/// <summary>
/// 必须继承自MarshalByRefObject父类,否则无法生成
/// </summary>
public class UserProcessor : MarshalByRefObject, IUserProcessor
{
public void RegUser(User user)
{
Console.WriteLine("用户已注册。用户名称{0} Password{1}", user.Name, user.Password);
}
}
}
Castle实现AOP
这个方法并不实用,但跟着网课稍微介绍一下
using Castle.DynamicProxy;//Castle.Core
namespace MyAOP
{
/// <summary>
/// 使用Castle\DynamicProxy 实现动态代理
/// 方法必须是虚方法
/// </summary>
public class CastleProxyAOP
{
public static void Show()
{
User user = new User()
{
Name = "Eleven",
Password = "123456"
};
ProxyGenerator generator = new ProxyGenerator();
MyInterceptor interceptor = new MyInterceptor();
UserProcessor userprocessor = generator.CreateClassProxy<UserProcessor>(interceptor);
userprocessor.RegUser(user);
}
public interface IUserProcessor
{
void RegUser(User user);
}
public class UserProcessor : IUserProcessor
{
/// <summary>
/// 必须带上virtual 否则无效~
/// </summary>
/// <param name="user"></param>
public virtual void RegUser(User user)
{
Console.WriteLine($"用户已注册。Name:{user.Name},PassWord:{user.Password}");
}
}
public class MyInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
PreProceed(invocation);
invocation.Proceed();
PostProceed(invocation);
}
public void PreProceed(IInvocation invocation)
{
Console.WriteLine("方法执行前");
}
public void PostProceed(IInvocation invocation)
{
Console.WriteLine("方法执行后");
}
}
}
}
使用EntLib\PIAB Unity 实现动态代理
首先将你的要代理的类注入到IUnityContainer容器,有两种方式
1
IUnityContainer container = new UnityContainer();
container.RegisterType<IUserProcessor, UserProcessor>();
IUserProcessor processor = container.Resolve<IUserProcessor>();
processor.RegUser(user);
2使用配置文件进行配置
//配置UnityContainer
IUnityContainer container = new UnityContainer();
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
UnityConfigurationSection configSection = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
configSection.Configure(container, "aopContainer");
//配置完容器,实例类并调用方法
IUserProcessor processor = container.Resolve<IUserProcessor>();
processor.RegUser(user);
User userNew1 = processor.GetUser(user);
User userNew2 = processor.GetUser(user);
这有这一块代码可能还看不出AOP在哪里,其实都体现在配置文件中
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<container name="aopContainer">
<extension type="Interception"/>
<register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP">
<interceptor type="InterfaceInterceptor"/> <!--这里是方式的选择,除了这里的接口还可以使用虚方法等,但不好用不介绍了-->
<!--下面就是功能件,UserProcessor实例执行方法前会先进入下面几个类-->
<interceptionBehavior type="MyAOP.UnityWay.ExceptionLoggingBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.CachingBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.LogBeforeBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.ParameterCheckBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.LogAfterBehavior, MyAOP"/>
</register>
<!--<register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="MyAOP.UnityWay.ExceptionLoggingBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.CachingBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.LogBeforeBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.ParameterCheckBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.LogAfterBehavior, MyAOP"/>
</register>-->
</container>
</containers>
</unity>
</configuration>
从这里就发现这个unity实现AOP的模式完全类似于管道中间件,那我们继续看“”中间件“”
using Unity.Interception.InterceptionBehaviors;
using Unity.Interception.PolicyInjection.Pipeline;
namespace MyAOP.UnityWay
{
/// <summary>
/// 不需要特性
/// </summary>
public class LogBeforeBehavior : IInterceptionBehavior
{
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("LogBeforeBehavior");
Console.WriteLine(input.MethodBase.Name);
foreach (var item in input.Inputs)
{
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item));
//反射&序列化获取更多信息
}
//这个不就相似与执行下一个中间件吗
return getNext().Invoke(input, getNext);//
}
public bool WillExecute
{
get { return true; }
}
}
}