C # aspect-oriented programming

Source: Internet
Author: User

AOP aspect-oriented programming (Aspect oriented programming) is a technology that implements the unified maintenance of program functions through precompilation and runtime dynamic agents. The core technology used in spring framework is AOP, which is a derivative of functional programming. The advantage of using AOP is that it can isolate the business logic, reduce the coupling degree, improve the reusability of the program, and improve the efficiency of the development. There are many open source AOP, and I use the KINGAOP here.

1 Project Structure

2 defining an entity class for a log record user and Loggingaspect slice log classes
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;} Ten public         System.DateTime logintime {get; set;}     }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 ov erride void OnEntry (Methodexecutionargs args) {one string logdata = Createlogdata ("Entering", args); 1          2 Console.WriteLine (logdata);}14 public override void OnExit (Methodexecutionargs args) 16         {Logdata string = Createlogdata ("Leaving", args); Console.WriteLine (logdata); 19         }20///&LT;SUMMARY&GT;21///AOP for log logic, simply modify it here, without modifying the processing class that is being sliced.//</summary>23  <param name= "Methodstage" ></param>24//<param name= "args" ></param>25//         <returns></returns>26 private String Createlogdata (string methodstage, Methodexecutionargs args) 27 {$ var str = new StringBuilder (); Str. Appendline (); Str. Appendline (String. Format (methodstage + "{0}", args. Method); (var argument in args.) Arguments): {argtype = argument. GetType (); Append (Argtype.name + ":"); PNs if (Argtype = = typeof (String) | | argtype.isprimitive). Append (argument),}41 else42 {+-foreach (var proper Ty in Argtype.getproperties ()). AppendFormat ("{0} = {1};", the property. Name, property. GetValue (argument, null));}48}49}50 return str. ToString (); 51}52}53}
3 Login Class

The class must implement the Getmetaobject method of the IDynamicMetaObjectProvider and label it with the property [Loggingaspect] on the method that requires the slice. The Loggingaspect property is the Loggingaspect slicing class that we defined above.

 1 using System.dynamic; 2 using System.Linq.Expressions; 3 using KINGAOP;     4 namespace aopdemo.logging 5 {6//<summary> 7//Login logic processing, just add a loggingaspect to realize the logging function, logical separation of logic and general processing 8 </summary> 9 Internal class Login:idynamicmetaobjectprovider10 {11//Add login Slice [Loggi Ngaspect] public void loginvaldate (User entity) 14 {15//requires only business logic processing, no log processing required F (Entity. Name = = "Jack" && entity. PWD = = "Wang") state = "logged";}20 ELSE21 {entity. State = "Error";}24}27//<summary>28///Idynamicmetaobj Implementation of Ectprovider//</summary>30//<param name= "parameter" ></param>31//&lt        ; returns></returns>32 public Dynamicmetaobject getmetaobject (Expression parameter) 33 {34     Need for AOP Weaving35 return new Aspectweaver (parameter, this); 36}37}38} 

The debug code is as follows:

1             //test sensation KINGAOP must have a dynamic to slice 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);

C # aspect-oriented programming

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.