clarification
This article is part of the OverallAuth 2.0 series of articles, which is updated weekly with an article in the series (Complete System Development from 0 to 1).
I will try to be very detailed in this system article, so that it can be understood by novice and veteran alike.
DESCRIPTION: OverallAuth 2.0 is a simple, easy to understand and powerful Permissions + Visual Process Management system.
Friendly reminder: this article is part of a series of articles, before reading that article, it is recommended to read the previous articles to better understand the structure of the project.
If you are interested, please follow me (*^▽^*).
Follow me. Beat me if you can't learn.
What is Aop Cutting Programming
As the saying goes, any system that doesn't use Aop is not a good system.
So what exactly is aop that people rate it so highly.
Aop is an acronym for Aspect Oriented Programming, which means "cutter-oriented programming".
Taken literally it means to cut a functional block into many sides.
Listed as:
I have 10 interfaces to fetch data, and as the code continues to iterate, I now want to do the following 2 operations.
1, now do a [performance monitoring] on these 10 interfaces to monitor the call time of these 10 interfaces.What would you do?
2, the 10 interfaces to do a [call monitoring], view the caller, call time, incoming parameters, return data and other records.What would you do?
Some say add a bit of monitoring code to each interface.
Others say to write a monitor method and call that method in the interface.
But this is not even a good choice, it is not only heavy workload and high coupling, but also prone to errors and not easy to maintain.
To solve this difficulty, aop was born. It slices the interface into multiple logical units without modifying the original logic of the interface.
It does a good job of reducing the coupling in this area and improving the flexibility and extensibility of the code.
Its current main roles are: logging, security control, exception handling, transacti...
The use of Aop in .net8
First things first: this use of Aop is used in conjunction with Autofac, so if you're too confused about Autofac, please move over to theBuilding a Permission Management System from 0 to 1 Series IV .
Installation: (select latest), (select latest, latest this seems to be included)
Write the AopPlugIn with the following code
/// <summary> /// aop plugin /// </summary> public class AopPlugIn : IInterceptor { /// <summary> /// interdiction /// </summary> /// <param name="invocation"></param> public void Intercept(IInvocation invocation) { //The name of the currently called method var methodName = ; //The name of the service where the method is currently called var interfaceServiceName = "I" + ; //Get information about the currently called method var methodInfo = ; //Number of current method parameters var methodParameterCount = ().Length; // All parameters of the current interface foreach (var parameter in ()) { //Parameter name var ParameterName = ; //parameter value var ParameterValue = []; //Parameter type var ParameterType = [] == null ? string.Empty : [].GetType().Name; } /* You can write any logic before the method is executed */ //Execute the calling method (); /* You can write any logic after the method is executed */ //Current Interface Return Value var value = ; } }
Code Explanation: The plugin is equivalent to an interceptor, as long as the service is registered by Autofac and requires interception, then all interfaces under the service will enter the interceptor.
Intercept():In this aop, you can get the name of the currently calling interface, service name, parameters, return value, etc.
(): Execute the currently called interface. Do some logical operations before and after the method, such as logging, performance monitoring, exception monitoring, etc.
Adding Aop Integration Services to Autofac
If Autofac is too unintelligible for you, please move toBuilding a Permission Management System from 0 to 1 Series IV .
After writing the plugin for aop, we need to integrate the aop plugin into Autofac to cooperate in accomplishing the interface interception. The code is as follows
/// <summary> /// Autofac Plugin /// </summary> public class AutofacPlugIn : { /// <summary> /// Overriding Autofac's Load method /// </summary> /// <param name="containerBuilder"></param> protected override void Load(ContainerBuilder containerBuilder) { //Service line program set Assembly service = ("DomainService"); Assembly intracface = ("Infrastructure"); //Register for aop (typeof(AopPlugIn)); //Items must end in xxx (service).Where(n => ("Service") && !) .InstancePerLifetimeScope().AsImplementedInterfaces().InterceptedBy(typeof(AopPlugIn)).EnableInterfaceInterceptors(); (intracface).Where(n => ("Repository") && !) .InstancePerLifetimeScope().AsImplementedInterfaces(); } }
Code Explanation: (typeof(AopPlugIn)); You have to register the aop first and then integrate it into Autofac via InterceptedBy(typeof(AopPlugIn)).EnableInterfaceInterceptors().
It is important to note here that we can add the where condition after to determine which interfaces need to be intercepted. If we don't add it, then all interfaces in Autofac will be intercepted.
beta (software)
The use of aop, in fact, is an interceptor, intercept was autofac injected into the service interface, so the configuration is very simple, of course, all the technology is not perfect, depending on the system to choose.
Execute the interface we built earlier
Execute: QueryAllUser interface GetAllUser()
As you can see, the interface is successfully intercepted. We are free to add business logic before and after the method, it does not change the original interface logic.
These are some basic information about the interface, and the return value, of course, we can get more information about the interface.
With this information, we can do a lot of logical operations, such as the previously mentioned: logging, performance monitoring, call monitoring, exception information, transaction processing and so on.
Above is the global exception catching mechanism, if you are interested, you can download the project and modify it.
Source code address: /yangguangchenjie/overall-auth2.0-web-api
Preview at http://139.155.137.144:8880/swagger/
Help me Star, please.
If you are interested, please pay attention to my weibo public number (*^▽^*).
Follow me: a full-stack multi-terminal treasure blogger, regularly sharing technical articles, irregularly sharing open source projects. Follow me to bring you to know a different program world!