In the previous article we have a simple understanding of the application scenarios of AOP and know the importance of AOP programming. This article we first read a piece of code, to start today's study.
recalls the code similar to the previous article: The Ispass of the Securityservice class determines that the user named "admin" has permission to save the data. OrderService to the class that holds the data, implements the IOrderService interface. public class Securityservice { public bool Ispass (string userName) { return userName = "admin"; { } public interface IOrderService { string U sername {get; set;} Object Save (object ID); } copy code
Implementation Section
OrderService public class Orderservice:iorderservice { & nbsp public string UserName {get; set.} public object Save (object ID) { return "Save:" + ID. ToString (); } Copy code
Aroundadvise public class Aroundadvise:imethodinterceptor { p Ublic Object Invoke (imethodinvocation invocation) { IOrderService target = (iorderservice) invocation. Target; Securityservice manager = new Securityservice (); if (manager. Ispass (target. UserName) { return INVO cation. Proceed (); else &NB Sp { return null; } } Copy code
Client part:
Program class program { static void Main (string[) args) { Proxyfactory factory = new Proxyfactory (new OrderService () {UserName = "admin"}); factory. Addadvice (New Aroundadvise ()); IOrderService service = (IOrderService) factory. GetProxy (); Object result = Service. Save (1); Console.WriteLine (result); console.readline (); } Copy code
Output: Save: 1
by learning from the previous article, we know what is aop---oriented programming. In AOP (aspect-oriented programming), when we write a program, we first think about what methods to intercept in the program, and what business to do after intercepting these methods. These concern processes, which we call: crosscutting concerns. The following concepts are derived from "crosscutting concerns":
Aspect (slice): The abstraction of a crosscutting concern is the slice, which is similar to a class, except that the focus is not the same, the class is an abstraction of the feature of the object, and the slice is the abstraction of the crosscutting concern. The slice in the program is the implementation part of the Aroundadvise class.
Joinpoint (connection point): The so-called connection point refers to those points that are intercepted. In Spring.net, a connection point refers to a method, because Spring.net only supports connection points of the method type, and Joinpoint (connection point) can also be a field or class builder. The connection point in the program is the method that is intercepted, such as the Save method of the OrderService class.
Pointcut (pointcut): The so-called pointcut refers to the definition of what we want to intercept those joinpoint (connection points). Invocation is not used in the program. method to determine which way to intercept, and Pointcut (Pointcut) is to intercept all methods.
Advice (notification): the so-called notification refers to the interception of Joinpoint (connection point) after the thing to do is to notify. Notifications are divided into pre notifications, post notifications, exception notifications, and surround notifications. Aroundadvise inherits the AopAlliance.Intercept.IMethodInterceptor interface, and the program uses a surround notification.
Target (target object): The destination object for the proxy. The target object in the program is the OrderService class, we pass proxyfactory factory = new Proxyfactory (new OrderService () {UserName = "admin"}); Copy Code
The
code determines that the target object is OrderService.
AOP Proxy: An object that is created by the AOP framework after the notification is applied to the target object. The proxy object created by the GetProxy () method in the program.
Weave: Refers to the process of applying a slice (aspect) to a target object (target) object and causing the proxy object to be created, known as weaving. As applied in the program, the OrderService class does not have the function of judging permissions, and we apply the function of judging the privilege--securityservice the Ispass method of the class to the target object.
Introduction (introduced): Introduction (introduced) without modifying class code: You can dynamically add some methods or fields to a class at run time. In the program does not modify the OrderService class, but in the runtime to judge the function of the authority through the Proxyfactory Addadvice method dynamically added.
These terms do not belong to the spring.net, but to the entire AOP programming. The so-called AOP, my understanding is that it should be such a process, first you need to define a slice, which is a class, where the method is the focus (also the notification), or the inside method is used to execute the target object method in the implementation of the need to execute the forward notification, post notification, exception notification, final notification, Surround notifications and more. With slices and notifications, to be applied to the target object, you need to define the entry point of these notifications, in other words, which methods are required to intercept, and the methods of interception are the connection points, the so-called connection point is the process of the dynamic execution, the method of weaving the plane ( Only methods can be intercepted in the spring.net at least. Therefore, the execution of notifications during the dynamic process is a weaving process, and the object that is being woven into these notifications is the target object.