. NET implements AOP and autofacaop through Autofac and DynamicProxy
What is AOP? Reference Baidu Encyclopedia: AOP is short for Aspect Oriented Programming. It is a technology that implements unified maintenance of program functions through pre-compilation and dynamic proxies during runtime. The implementation of AOP mainly consists of two methods, one is static implantation during compilation, the advantage is high efficiency, the disadvantage is the lack of flexibility, under. net postsharp representative (this is a charge ). The other method is dynamic proxy, which has the opposite advantages and disadvantages. It dynamically creates a proxy for the target type and intercepts it through proxy calls. What can AOP do? Common Use Cases include transaction processing and logging. Next, let's talk about how Autofac implements AOP. Autofac is a very good IOC container with excellent performance under. net (the most efficient container under. net), and AOP is a huge addition. Autofac's AOP is implemented through the core part of the Castle (also a container) project, called Autofac. Extras. DynamicProxy. As the name suggests, its implementation method is dynamic proxy.
Preparations before use:
Install the package using Nuge: Autofac, Autofac. Extras. DynamicProxy. After successful installation, three references will be added.
The following is the official start!
Step 1: Create an interceptor
The following is a simple interceptor example. The interceptor function is to display the intercepted method name, parameter list, and returned results.
1 /// <summary> 2 // the interceptor must implement the IInterceptor interface Intercept method 3 /// </summary> 4 public class CallLogger: IInterceptor 5 {6 TextWriter _ output; 7 8 public CallLogger (TextWriter output) 9 {10 _ output = output; 11} 12 13 // <summary> 14 // The blocking method prints the name, parameters, and returned results of the intercepted method before execution. /// </ summary> 16 // <param name = "invocation"> contains information about the blocked method </param> 17 public void Intercept (IInvocation invocation) 18 {19 20_output . WriteLine ("You are calling Method \" {0} \ "parameter is {1 }... ", 21 invocation. method. name, 22 string. join (",", invocation. arguments. select (a => (?? ""). ToString ()). toArray (); 23 24 // After the intercepted method is executed, continue to execute 25 invocation. proceed (); 26 27 _ output. writeLine ("method execution completed, return result: {0}", invocation. returnValue); 28} 29}
Step 2: register the Interceptor to the Autofac container
The interceptor must be registered in the Aufofac container and can be injected by the interceptor type or name. These two methods will make the method of using the interceptor different (as described later ).
1 // name injection 2 builder. register (c => new CallLogger (Console. out) 3. named <IInterceptor> ("log-CILS"); 4 5 // Type Injection 6 builder. register (c => new CallLogger (Console. out ));
Step 3: Enable interceptor
EnableInterfaceInterceptors () and EnableClassInterceptors ().
The EnableInterfaceInterceptors method dynamically creates an interface proxy.
The EnableClassInterceptors method creates a subclass proxy class of the target class. Here, you must note that only virtual methods are intercepted and the method is overwritten.
Sample Code for enabling Interceptor:
// Enable class proxy interception builder. RegisterType <Circle> (). EnableClassInterceptors (); // enable interface proxy interception builder. RegisterType <Circle> (). EnableInterfaceInterceptors ();
Step 4: specify the type of Interception
There are two methods:
First: Add Attribute to the type
Type 2: dynamically inject interceptor when registering the type to the container
1 // Dynamic Injection interceptor CallLogger2 builder. RegisterType <Circle> (). InterceptedBy (typeof (CallLogger). EnableClassInterceptors ();
Step 5: test the effect
1. Class proxy Interception
Circle code:
2. Interface proxy Interception
IShape interface code:
1 public interface IShape2 {3 /// <summary> 4 /// shape Area 5 /// </summary> 6 void Area (); 7 8}
Circle code:
1 public class Circle: IShape2 {3 // override the parent class abstract Method 4 public void Area () 5 {6 Console. writeLine ("you are calling the method for area calculation"); 7} 8}
If you have any questions, you are welcome to ask questions.