C # getting started with the AOP framework,

Source: Internet
Author: User

C # getting started with the AOP framework,

Aspect Oriented Programming (AOP) is a technology that implements unified maintenance of program functions by means of pre-compilation and dynamic proxies in runtime. The core technology used by the Spring framework is AOP, which is a derivative model of functional programming. The advantage of using AOP is that business logic can be isolated to reduce coupling, improve program reusability, and improve development efficiency. There are also a lot of open-source AOP. I use KingAOP here.

1. Project Structure

2 define the entity class of a log record User and LoggingAspect slice log class
 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. 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 you only need to modify the logon log logic here, you do not need to modify the processing class 22 of the section. /// </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 else42 {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}
3. Login class

This class must implement the GetMetaObject method of IDynamicMetaObjectProvider, and label it with the attribute [LoggingAspect] on the method to be cut. The LoggingAspect attribute is the LoggingAspect slicing processing class defined above.

1 using System. dynamic; 2 using System. linq. expressions; 3 using KingAOP; 4 namespace AOPDemo. logging 5 {6 /// <summary> 7 // log logic processing. You only need to add a LoggingAspect to implement the log function, achieve logical separation of general processing 8 // </summary> 9 internal class Login: IDynamicMetaObjectProvider10 {11 // Add login section 12 [LoggingAspect] 13 public void LoginValdate (User entity) 14 {15 // you only need to process the business logic, without the need for log processing 16 if (entity. name = "jack" & entity. pwd = "wang") 17 {18 entity. state = "Logged"; 19} 20 else21 {22 entity. state = "Error "; 23} 24 25 26} 27 /// <summary> 28 // IDynamicMetaObjectProvider implementation 29 /// </summary> 30 /// <param name = "parameter"> </param> 31 // <returns> </returns> 32 public DynamicMetaObject GetMetaObject (Expression parameter) 33 {34 // need for AOP weaving35 return new AspectWeaver (parameter, this); 36} 37} 38}

The debugging code is as follows:

1 // test the result. KingAOP must have a dynamic before it can be split to 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 );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.