AOP(面向切面编程)是一种编程思想,可以说是OOP(面向对象编程)的一种扩展,AOP利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的行为封装到一个可重用模块,例如事务处理、日志管理、权限控制等封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
利用“Unity”框架封装可重用代码块:
1.首先在项目中引入NuGet包“Uniry”:
Unity包.png
2.编写重用代码块(继承IInterceptionBehavior):
///横向方法
public class MoniterBehavior: IInterceptionBehavior
{
public bool WillExecute
{
get { return true; }
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
//重用代码块
//方法执行前执行操作...
//根据配置文件执行下一模块方法,若没有下一模块,则执行当前方法
var method = getNext().Invoke(input, getNext);
//方法执行后执行操作
return method ;
}
}
///
///异常日志模块
///
public class ExceptionLoggingBehavior : IInterceptionBehavior
{
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("ExceptionLoggingBehavior");
IMethodReturn methodReturn = getNext()(input, getNext);
if (methodReturn.Exception == null)
{
Console.WriteLine("无异常");
}
else
{
Console.WriteLine($"异常:{methodReturn.Exception.Message}");
}
return methodReturn;
}
public bool WillExecute
{
get { return true; }
}
}
///
///参数校验模块
///
public class ParameterCheckBehavior : IInterceptionBehavior
{
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("ParameterCheckBehavior");
//通过特性校验
User user = input.Inputs[0] as User;
if (user.Password.Length < 10)
{
return input.CreateExceptionMethodReturn(new Exception("密码长度不能小于10位"));
}
else
{
Console.WriteLine("参数检测无误");
return getNext().Invoke(input, getNext);
}
}
public bool WillExecute
{
get { return true; }
}
}
3.编写Unity配置文件:
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
<!--Microsoft.Practices.Unity.Configuration.UnityConfigurationSection-->
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<!-- 容器名称(用于前台绑定)-->
<container name="aopContainer">
<extension type="Interception"/>
<!--接口映射(具体的接口对应实际的业务类,DLL名称) -->
<register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP">
<interceptor type="InterfaceInterceptor"/>
<!--重用代码块的位置及类库名,执行顺序会按照目前排列执行 -->
<interceptionBehavior type="MyAOP.UnityWay.MoniterBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.LogBeforeBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.ParameterCheckBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.CachingBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.ExceptionLoggingBehavior, MyAOP"/>
<interceptionBehavior type="MyAOP.UnityWay.LogAfterBehavior, MyAOP"/>
</register>
</container>
</containers>
</unity>
</configuration>
4.客户类使用Unity框架,并实现自己的业务类:
//上面配置文件绑定的映射接口
public interface IUserProcessor
{
void RegUser(User user);
User GetUser(User user);
}
//上面配置文件绑定的实体类
public class UserProcessor : IUserProcessor
{
public void RegUser(User user)
{
Console.WriteLine("业务逻辑代码。");
}
public User GetUser(User user)
{
return user;
}
}
//实体参数类
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
}
//客户程序代码
public static void UnityAop()
{
User user = new User()
{
Name = "ZhangSan",
Password = "123456"
};
//加载Unity.Config配置文件
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");
//根据配置文件初始化UserProcessor对象
IUserProcessor processor = container.Resolve<IUserProcessor>();
//调用方法
processor.RegUser(user);
processor.GetUser(user);
}
网友评论