AOP--面向切面编程
1.使用代理模式实现AOP
public class UserProcessor : IUserProcessor { public void RegUser(User user) { Console.WriteLine("用户已注册。Name:{0},Password :{1}", user.Name, user.Password); } } /// <summary> /// 代理模式实现AOP /// </summary> public class ProxyUserProcessor : IUserProcessor { private IUserProcessor _UserProcessor = new UserProcessor(); public void RegUser(User user) { BeforeProceed(user); this._UserProcessor.RegUser(user); AfterProceed(user); } private void AfterProceed(User user) { Console.WriteLine("方法执行后"); } private void BeforeProceed(User user) { Console.WriteLine("方法执行前"); } }
2.装饰器模式实现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); } private void AfterProceed(User user) { Console.WriteLine("方法执行后"); } private void BeforeProceed(User user) { Console.WriteLine("方法执行前"); } }
3.Unity实现AOP
调用如下:需读配置文件
IUnityContainer container = new UnityContainer(); ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = Path.Combine(@"D:\C#高级学习\AOP\MyAOP\MyAOP\CfgFiles\Unity.Config"); Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); UnityConfigurationSection configSelction = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName); configSelction.Configure(container, "aopContainer"); IUserProcessor processor = container.Resolve<IUserProcessor>(); processor.RegUser(user); processor.GetUser(user); processor.GetUser(user);
配置文件如下:
<?xml version="1.0" encoding="utf-8" ?> <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"/> <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>
具体的类如下:
如遇到getNext().Invoke,就会执行后续的步骤,后续的步骤是指配置文件中下一个类。
public class LogBeforeBehavior:IInterceptionBehavior { public bool WillExecute { get { return true; } } public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Console.WriteLine("logbeforbehavior"); foreach (var item in input.Inputs)//input.MethodBase { Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(item)); } return getNext().Invoke(input, getNext); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库