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 namespaceaopdemo.logging2 {3 classUser4 {5 Public intID {Get;Set; }6 Public stringName {Get;Set; }7 Public stringpwd{Get;Set;}8 Public stringIP {Get;Set; }9 Public stringState {Get;Set; }Ten PublicSystem.DateTime Logintime {Get;Set; } One A } -}
1 usingSystem;2 usingSystem.Text;3 usingkingaop.aspects;4 5 namespaceaopdemo.logging6 {7 Internal classLoggingaspect:onmethodboundaryaspect8 {9 Public Override voidonentry (Methodexecutionargs args)Ten { One stringLogdata = Createlogdata ("Entering", args); A Console.WriteLine (logdata); - } - the Public Override voidOnExit (Methodexecutionargs args) - { - stringLogdata = Createlogdata ("Leaving", args); - Console.WriteLine (logdata); + } - /// <summary> + ///AOP for logon log logic, you can only modify it here, without modifying the processing class that is being sliced A /// </summary> at /// <param name= "Methodstage" ></param> - /// <param name= "args" ></param> - /// <returns></returns> - Private stringCreatelogdata (stringmethodstage, Methodexecutionargs args) - { - varstr =NewStringBuilder (); in Str. Appendline (); -Str. Appendline (string. Format (Methodstage +"{0}", args. Method)); to foreach(varArgumentinchargs. Arguments) + { - varArgtype =argument. GetType (); the *Str. Append (Argtype.name +": "); $ Panax Notoginseng if(Argtype = =typeof(string) ||argtype.isprimitive) - { the Str. Append (argument); + } A Else the { + foreach(varPropertyinchargtype.getproperties ()) - { $Str. AppendFormat ("{0} = {1};", $Property. Name, property. GetValue (argument,NULL)); - } - } the } - returnStr. ToString ();Wuyi } the } -}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 usingsystem.dynamic;2 usingSystem.Linq.Expressions;3 usingKINGAOP;4 namespaceaopdemo.logging5 {6 /// <summary>7 ///login Logic processing, just add a loggingaspect can realize the log function, to achieve logical separation of logic and general processing8 /// </summary>9 Internal classLogin:idynamicmetaobjectproviderTen { One //Adding a login slice A [Loggingaspect] - Public voidloginvaldate (User entity) - { the //only business logic is needed, no log processing is required - if(Entity. Name = ="Jack"&& entity. PWD = ="Wang") - { -Entity. State ="logged"; + } - Else + { AEntity. State ="Error"; at } - - - } - /// <summary> - ///the realization of IDynamicMetaObjectProvider in /// </summary> - /// <param name= "parameter" ></param> to /// <returns></returns> + Publicdynamicmetaobject getmetaobject (Expression parameter) - { the //need for AOP weaving * return NewAspectweaver (parameter, This); $ }Panax Notoginseng } -}
The debug code is as follows:
1 // Test sensation KINGAOP must have a dynamic to slice the 2 logging.login test = new Logging.login (); 3 dynamic entity = new logging.user {Name = " jon Span style= "color: #800000;" > ", ID = 99 , Pwd=" wang , State=, logintime=< Span style= "color: #000000;" >system.datetime.now}; 4 test. Loginvaldate (entity);
Getting Started with the C # AOP framework