AOP面向切面編程(Aspect Oriented Programming),是通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。Spring框架用的核心技術就是AOP,是函數式編程的一種衍生范型。利用AOP的好處就是可以對業務邏輯進行隔離,降低耦合度,提高程序的可重用性,同時提高了開發的效率。開源的AOP也有不少,我這裡用的KingAOP.
1 namespace AOPDemo.Logging 2 { 3 class User 4 { 5 public int ID { get; set; } 6 public string Name { get; set; } 7 public string Pwd{get;set;} 8 public string IP { get; set; } 9 public string State { get; set; } 10 public System.DateTime LoginTime { get; set; } 11 12 } 13 }
1 using System; 2 using System.Text; 3 using KingAOP.Aspects; 4 5 namespace AOPDemo.Logging 6 { 7 internal class LoggingAspect : OnMethodBoundaryAspect 8 { 9 public override void OnEntry(MethodExecutionArgs args) 10 { 11 string logData = CreateLogData("Entering", args); 12 Console.WriteLine(logData); 13 } 14 15 public override void OnExit(MethodExecutionArgs args) 16 { 17 string logData = CreateLogData("Leaving", args); 18 Console.WriteLine(logData); 19 } 20 /// <summary> 21 /// AOP對於登錄日志邏輯,只需在此進行修改即可,無需修改被切面的處理類 22 /// </summary> 23 /// <param name="methodStage"></param> 24 /// <param name="args"></param> 25 /// <returns></returns> 26 private string CreateLogData(string methodStage, MethodExecutionArgs args) 27 { 28 var str = new StringBuilder(); 29 str.AppendLine(); 30 str.AppendLine(string.Format(methodStage + " {0} ", args.Method)); 31 foreach (var argument in args.Arguments) 32 { 33 var argType = argument.GetType(); 34 35 str.Append(argType.Name + ": "); 36 37 if (argType == typeof(string) || argType.IsPrimitive) 38 { 39 str.Append(argument); 40 } 41 else 42 { 43 foreach (var property in argType.GetProperties()) 44 { 45 str.AppendFormat("{0} = {1}; ", 46 property.Name, property.GetValue(argument, null)); 47 } 48 } 49 } 50 return str.ToString(); 51 } 52 } 53 }
該類必須實現IDynamicMetaObjectProvider的GetMetaObject方法,同時在需要切面的方法上用屬性[LoggingAspect]進行標注,LoggingAspect屬性也就是我們上面定義的LoggingAspect切片處理類.
1 using System.Dynamic; 2 using System.Linq.Expressions; 3 using KingAOP; 4 namespace AOPDemo.Logging 5 { 6 /// <summary> 7 /// 登錄邏輯處理,只需添加一個LoggingAspect即可實現日志功能,達到邏輯和通用處理的邏輯分離 8 /// </summary> 9 internal class Login : IDynamicMetaObjectProvider 10 { 11 //添加登錄切面 12 [LoggingAspect] 13 public void LoginValdate(User entity) 14 { 15 //只需進行業務邏輯處理,無需進行日志處理 16 if (entity.Name == "jack" && entity.Pwd == "wang") 17 { 18 entity.State = "Logged"; 19 } 20 else 21 { 22 entity.State = "Error"; 23 } 24 25 26 } 27 /// <summary> 28 /// IDynamicMetaObjectProvider的實現 29 /// </summary> 30 /// <param name="parameter"></param> 31 /// <returns></returns> 32 public DynamicMetaObject GetMetaObject(Expression parameter) 33 { 34 //need for AOP weaving 35 return new AspectWeaver(parameter, this); 36 } 37 } 38 }
調試代碼如下:
1 //測試感覺KingAOP必須有一個dynamic才能切面 2 Logging.Login test = new Logging.Login(); 3 dynamic entity = new Logging.User { Name = "Jon", ID = 99,Pwd="wang",State="",LoginTime=System.DateTime.Now}; 4 test.LoginValdate(entity);