ASP. NETCore Aspect Programming AOP Details
I AOP Overview:
2、 AOP implementation:
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! " ); } }
/// <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; } }
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; } }
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 " ; } }