最開始接觸AOP這個概念,是在大學Java課程中(具體哪本忘記了,JavaWeb?)接觸到的,當時的理解就是,一個請求過來,自上而下,突然從中間切一刀,從那個圖是這樣理解的,文字描述的都忘記了,關于AOP的博客有好多,在作業中需要用到,我也是看著博客,外加視頻學習來理解的,
http://wayfarer.cnblogs.com/articles/241012.html
這篇博客,寫的還是蠻詳細的,下面只是我自己的總結,
AOP不是一種設計模式,而是一種編程思想,和POP,OOP一樣,是OOP的擴展,AOP的出現并不能代替OOP,
POP,面向程序編程:
符合邏輯思維,線性的處理問題,但是無法應對復雜的系統
OOP面向物件編程:
萬物皆物件,物件互動完成功能,功能疊加成模塊,模塊組成系統,才有機會搭建復雜的大型的軟體系統,
下面以一個例子來作為對比:
磚塊--------墻---------房間---------大廈
類--------功能點------模塊---------系統
磚塊應該是穩定的,說明是靜態,不變的,在程式開發的程序中,類確實會變化的,增加日志/例外/權限/快取/事務等,只能修改類,
在GOF23種設計模式,應對變化的,核心套路是依賴抽象,細節就可以變化,但是只能替換整個物件,沒辦法把一個類動態改變,

AOP面向切面編程:
允許開發者動態的修改靜態的OO模型,就像現實生活中物件在生命周期中會不斷的改變自身,AOP是一種編程思想,是OOP思想的補充,
正式因為能夠動態的擴展功能,所以在程式設計的時候,就可以有以下好處:
1、聚焦核心業務邏輯,權限/例外/快取/事務,通過功能可以通過AOP方式添加,程式設計簡單,
2、動態擴展,集中管理,代碼復用,規范化,
下面,用裝飾器模式,去實作一個AOP功能:

/// <summary> /// 裝飾器模式實作靜態代理 /// AOP 在方法前后增加自定義的方法 /// </summary> public class DecoratorAOP { public static void Show() { User user = new User() { Name = "bingle", Password = "123123123123" }; IUserProcessor processor = new UserProcessor(); processor.RegUser(user); Console.WriteLine("***************"); processor = new UserProcessorDecorator(processor); processor.RegUser(user); } 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) { this._UserProcessor = userprocessor; } public void RegUser(User user) { BeforeProceed(user); this._UserProcessor.RegUser(user); AfterProceed(user); } /// <summary> /// 業務邏輯之前 /// </summary> /// <param name="user"></param> private void BeforeProceed(User user) { Console.WriteLine("方法執行前"); } /// <summary> /// 業務邏輯之后 /// </summary> /// <param name="user"></param> private void AfterProceed(User user) { Console.WriteLine("方法執行后"); } } }View Code
實作AOP的多種方式:
1、靜態實作----裝飾器/代理模式
2、動態實作----Remoting/Castlet
3、靜態植入---PostSharp(收費)----擴展編譯工具,生成的加入額外代碼
4、依賴注入容器的AOP擴展(Unity)
5、MVC的Filter---特性標機,然后該方法執行前后就多了邏輯
之前看到有的人認為,在.NET Core中的中間件,也是AOP的一種實作,也有一些人認為不是,博主認為,.NET Core中的中間件并不是AOP的一種實作,等后續隨筆記載到中間件的時候,再去詳細說明吧,
依賴注入容器的AOP擴展(擴展)
基于組態檔的Unity,
首先,用Nuget引入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"/> <register type="MyAOP.UnityWay.IUserProcessor,MyAOP" mapTo="MyAOP.UnityWay.UserProcessor,MyAOP"> <interceptor type="InterfaceInterceptor"/> <interceptionBehavior type="MyAOP.UnityWay.MonitorBehavior, 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>View Code

使用EntLib\PIAB Unity 實作動態代理:

public class UnityConfigAOP { [Obsolete] public static void Show() { User user = new User() { Name = "bingle", Password = "1234567890123456789" }; //配置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); processor.GetUser(user); } }View Code

public class LogAfterBehavior : IInterceptionBehavior{ public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Console.WriteLine("LogAfterBehavior"); foreach (var item in input.Inputs) { Console.WriteLine(item.ToString());//反射獲取更多資訊 } IMethodReturn methodReturn = getNext()(input, getNext); Console.WriteLine("LogAfterBehavior" + methodReturn.ReturnValue); return methodReturn; } public bool WillExecute { get { return true; } }}View Code

/// <summary> /// 不需要特性 /// </summary> public class LogBeforeBehavior : IInterceptionBehavior { public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Console.WriteLine("LogBeforeBehavior"); foreach (var item in input.Inputs) { Console.WriteLine(item.ToString());//反射獲取更多資訊 } return getNext().Invoke(input, getNext); } public bool WillExecute { get { return true; } } }View Code

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; } } }View Code

/// <summary>/// 不需要特性/// </summary>public class CachingBehavior : IInterceptionBehavior{ public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Console.WriteLine("CachingBehavior"); //input.Target.GetType().GetCustomAttributes() if (input.MethodBase.Name.Equals("GetUser")) return input.CreateMethodReturn(new User() { Id = 234, Name = "Eleven" }); return getNext().Invoke(input, getNext); } public bool WillExecute { get { return true; } }}View Code

/// <summary>/// 性能監控的AOP擴展/// </summary>public class MonitorBehavior : IInterceptionBehavior{ public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { Console.WriteLine(this.GetType().Name); string methodName = input.MethodBase.Name; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var methodReturn = getNext().Invoke(input, getNext);//后續邏輯執行 stopwatch.Stop(); Console.WriteLine($"{this.GetType().Name}統計方法{methodName}執行耗時{stopwatch.ElapsedMilliseconds}ms"); return methodReturn; } public bool WillExecute { get { return true; } }}View Code
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/44333.html
標籤:設計模式
上一篇:設計模式-責任鏈設計模式
下一篇:觀察者模式,從公眾號群發說起

