ASP. NETCore Aspect Programming AOP Details

I AOP Overview:

AOP (Aspect Oriented Programming), namely Aspect oriented programming Using the idea of "crosscutting" system function and Business function separate.

   The system functions mainly include

     1. Cache module : Used to cache data, which has little to do with system business functions, and is used to improve system performance. Whether to read from the cache when the data is requested. When there is no data in the cache,

Whether to put the acquired data into the cache so that it can be read from the cache when the next data request is made. Common cache tools include monogodb and Redis.

     2. Verification module : Determine whether the current user has the permission to obtain the data or perform the operation.

     3. Log module : Collect the operation traces of the current user or record the problems of the system, and conduct big data behavior analysis later.

        Business function: The normal logic function of the system to handle user requests.

After the http request comes, it will first go through the crosscutting interception of the system function before it can be processed by the business function. The specific process of AOP is shown in the figure below:

         

2、 AOP implementation:

Domestic tycoons are based on NETStand implements an AOP framework, and its source code address is https://github.com/dotnetcore/AspectCore-Framework

To use this framework, Nuget AspectCore. Extensions. DependencyInjection is referenced into the project, which can implement log interception and cache interception. The framework is based on IOC containers.

Here, I use the console program to demonstrate: first, nuget references AspectCore Extensions. DependencyInjection。

1. Program entry:  

 class Program { static  void Main( string [] args) { // Create IOC container ServiceCollection for registering services ServiceCollection services = new ServiceCollection(); // Register the service in the container
 services. ConfigureDynamicProxy(); services. AddScoped <IMySql, MySql> (); // Create a ServiceProvider to obtain services.
             var provider = services. BuildDynamicProxyProvider(); // Get services.
             var mysql = provider. GetService<IMySql> (); // Perform services.
             var msg=mysql. GetById( ten ); // Data removal for the first time, data retrieval from business logic
             var value = mysql. GetData( " hehe " ); Console. WriteLine(value); // Data is removed for the second time and retrieved from the cache value = mysql. GetData( " hehe " ); Console. WriteLine(value); Console. ReadKey(); Console. WriteLine( " Hello World! " ); } }

 

2. AOP:

Log aspect:

     ///  <summary>
     /// Log module: (log aspect, log AOP) ///  </summary>
     public  class MyLogInterceptorAttribute : AbstractInterceptorAttribute { ///  <summary>
         /// 
         ///  </summary>
         ///  <param name="context"> The parameter of the delegate. The GetById method is passed here </param>
         ///  <param name="next"> Delegate. The action of delegation depends on the specific action of the incoming method </param>
         ///  <returns></returns>
         public  override Task Invoke(AspectContext context, AspectDelegate next) { Console. WriteLine( " Start Logging " ); // var aa = context; // var bb = next; var task = next(context); // Start from here to implement the specific method. GetById is executed here; Console. WriteLine( " End Log " ); return task; } }

  Cache facets:

 public  class MyCacheInterceptorAttribute : AbstractInterceptorAttribute { // For simulation cache
         private Dictionary< string , string > CacheDic = new Dictionary< string , string > (); ///  <summary>
         /// 
         ///  </summary>
         ///  <param name="context"> The parameter of the delegate. The GetById method is passed here </param>
         ///  <param name="next"> Delegate, the action of delegation, depends on the specific action of the incoming method </param>
         ///  <returns></returns>
         public  override Task Invoke(AspectContext context, AspectDelegate next) { // Get the AspectContext passed in, that is, the parameter of the method passed in.
             var cacheKey = string .Join( " , " , context. Parameters); // Data is removed from the cache. If cache exists, return
             if (CacheDic.ContainsKey(cacheKey)) { context. ReturnValue = CacheDic[cacheKey]. ToString(); return Task. CompletedTask; } // No cache. Get data from the business logic, and put the data into the cache after getting the data
             var task = next(context); var cacheValue = context. ReturnValue. ToString(); CacheDic. Add(cacheKey, " From cache: " + cacheValue); return task; } }

  

  3. Business function:

     public  interface IMySql { string GetById( int id); string GetData( string key); } public  class MySql : IMySql { [MyLogInterceptor] // Log Aspect
         public  string GetById( int id) { var msg = $ " Data with ID of {id} has been obtained " ; Console. WriteLine(id); return msg; } [MyCacheInterceptor] // Cache facets
         public  string GetData( string key) { return  " get data " ; } }

 

posted @ 2020-09-14 00:41   FrankFyy   Reading( two thousand five hundred and fifty-two Comments( zero edit   Collection   report