Home>

Aop aspect oriented programming is a technology that implements unified maintenance of program functions through pre-compilation and dynamic proxy at runtime.The core technology used by the spring framework is AOP, which is a derivative paradigm of functional programming.The advantage of using aop is that it can isolate business logic.Reduce coupling,Improve program reusability,At the same time, the efficiency of development is improved.There are also a lot of open source aops.I use kingaop here.

1 Project structure

2 Define a logging entity class user and loggingaspect aspect log class

namespace aopdemo.logging
{
 class user
 {
  public int id {get;set;}
  public string name {get;set;}
  public string pwd {get;set;}
  public string ip {get;set;}
  public string state {get;set;}
  public system.datetime logintime {get;set;}
 }
}
using system;
using system.text;
using kingaop.aspects;
namespace aopdemo.logging
{
 internal class loggingaspect:onmethodboundaryaspect
 {
  public override void onentry (methodexecutionargs args)
  {
   string logdata=createlogdata ("entering", args);
   console.writeline (logdata);
  }
  public override void onexit (methodexecutionargs args)
  {
   string logdata=createlogdata ("leaving", args);
   console.writeline (logdata);
  }
  ///<summary>
  ///aop for login log logic,Just modify it here,No need to modify the processing class of the aspect
  ///</summary>
  ///<param name="methodstage"></param>
  ///<param name="args"></param>
  ///<returns></returns>
  private string createlogdata (string methodstage, methodexecutionargs args)
  {
   var str=new stringbuilder ();
   str.appendline ();
   str.appendline (string.format (methodstage + "{0}", args.method));
   foreach (var argument in args.arguments)
   {
    var argtype=argument.gettype ();
    str.append (argtype.name + ":");
    if (argtype == typeof (string) || argtype.isprimitive)
    {
     str.append (argument);
    }
    else
    {
     foreach (var property in argtype.getproperties ())
     {
      str.appendformat ("{0}={1};",       property.name, property.getvalue (argument, null));
     }
    }
   }
   return str.tostring ();
  }
 }
}

3 login class

This class must implement the getmetaobject method of idynamicmetaobjectprovider, and mark it with the attribute [loggingaspect] on the method that requires the aspect,The loggingaspect attribute is also the loggingaspect slice processing class we defined above.

using system.dynamic;
using system.linq.expressions;
using kingaop;
namespace aopdemo.logging
{
 ///<summary>
 ///login logic processing,Just add a loggingaspect to implement the logging function,Achieve logical separation of logic and general processing
 ///</summary>
 internal class login:idynamicmetaobjectprovider
 {
  //Add login aspect
  [loggingaspect]
  public void loginvaldate (user entity)
  {
   //Just do business logic processing,No log processing required
   if (entity.name == "jack"&&entity.pwd == "wang")
   {
    entity.state="logged";
   }
   else
   {
    entity.state="error";
   }
  }
  ///<summary>
  ///Implementation of idynamicmetaobjectprovider
  ///</summary>
  ///<param name="parameter"></param>
  ///<returns></returns>
  public dynamicmetaobject getmetaobject (expression parameter)
  {
   //need for aop weaving
   return new aspectweaver (parameter, this);
  }
 }
}

The debug code is as follows:

//The test feels that kingaop must have a dynamic in order to cut
logging.login test=new logging.login ();
dynamic entity=new logging.user {name="jon", id=99, pwd="wang", state="", logintime=system.datetime.now};
test.loginvaldate (entity);
c
  • Previous Method to solve the problem that angular $httppost () cannot receive parameter values ​​when submitting data in the background
  • Next Simple application of AJAX technology in PHP development