AOP, short for aspect oriented programming, is a continuation of OOP. It is a technology that dynamically and uniformly adds functions to the program without modifying the source code through the pre-compilation method and the dynamic agent at runtime, it looks at the structure of a program from a different perspective than OOP: Oop splits an application {
Tagshow (Event)
} "> The series is represented as objects of the inheritance relationship. AOP splits the program into a series of aspects (Aspects) or concerns (concerns ). AOP will {
Tagshow (Event)
} "> Transaction Management focuses that are originally horizontally distributed across multiple objects are modularized (these concerns are also known as cross-cutting (crosscutting) concerns ). In spring. NET provides rich support for Aspect-Oriented Programming, allowing internal development by separating the application's business logic and system-level services (such as audit and transaction management. Application objects only implement what they should do -- complete the business logic -- that's all. They are not responsible (or even conscious) for other system-level concerns, such as log or transaction support.
Here is an example to illustrate all of this:
Scenario: when calling the Save method, the business-class companymanager needs to call the securitymanager class to determine whether the permission is sufficient (figure 1 ).
Attachment: 2009-11-12.1.gif
Figure 1
Prerequisites:
- Public class companydao
- {
- Public void save ()
- {
- Console. writeline ("save data ");
- }
- }
- Public interface icompanymanager
- {
- String username {Get; set ;}
- Void save ();
- }
- Public interface isecuritymanager
- {
- Bool ispass (string username );
- }
Copy code
Securitymanager
- Public class securitymanager: isecuritymanager
- {
- /** // <Summary>
- /// Determine the permission
- /// </Summary>
- /// <Param name = "username"> </param>
- /// <Returns> </returns>
- Public bool ispass (string username)
- {
- Return username = "admin ";
- }
- }
Copy code
The first method is to call isecuritymanager {
Tagshow (Event)
} "> The ispass method of the interface determines the permission.
Simplecompanymanager
- Public class simplecompanymanager: icompanymanager
- {
- Attributes that can be injected externally # attributes that can be injected externally by region
- Public String username {Get; set ;}
- Public companydao Dao {Get; set ;}
- # Endregion
- Public void save ()
- {
- // Determine the permission
- Isecuritymanager SECURITY = new securitymanager ();
- If (Security. ispass (username ))
- {
- // Execute the Business Method
- //.
- // Call the DaO Layer Method
- Dao. Save ();
- }
- Else
- {
- // Execute other business methods
- Console. writeline ("you do not have this permission ");
- }
- }
- }
Copy code
In this way, the companymanager class is coupled with isecuritymanager or securitymanager in business. Smart friends will find that in gof ({
Tagshow (Event)
} "> There is a mode (proxy mode) in the design mode to remove this coupling.
Proxy pattern: What is proxy pattern? A proxy object is provided for an object, and the reference to the source object is controlled by the proxy object. An agent is an action taken by one person or institution on behalf of another person or institution. In some cases, the customer does not want or cannot directly reference an object. The proxy object can serve as an intermediary between the customer and the target object. The client cannot identify the proxy topic object and the real topic object. The proxy mode does not know the real proxy object, but only holds an interface of the proxy object. At this time, the proxy object cannot be created as the proxy object, other roles of the system must be created and passed in as proxies (figure 2 ).
Attachment: 2009-11-12.3.gif
Figure 2
Companymanager
- Public class companymanager: icompanymanager
- {
- Attributes that can be injected externally # attributes that can be injected externally by region
- Public String username {Get; set ;}
- Public companydao Dao {Get; set ;}
- # Endregion
- Public void save ()
- {
- // Execute the Business Method
- //.
- // Call the DaO Layer Method
- Dao. Save ();
- }
- }
Copy code
Companyproxymanager
- Public class companyproxymanager: icompanymanager
- {
- Public String username {Get; set ;}
- Private icompanymanager target = new companymanager ();
- Public void save ()
- {
- // Determine the permission
- Isecuritymanager SECURITY = new securitymanager ();
- If (Security. ispass (username ))
- {
- // Call the Save method of the target object
- Target. Save ();
- }
- Else
- {
- Console. writeline ("you do not have this permission ");
- }
- }
- }
Copy code
In this way, the companymanager class does not have to be coupled with the securitymanager class for permission judgment, but this method is difficult to implement.
The third implementation method is the combination of the AOP: aopalliance. Intercept. imethodinterceptor interface and proxyfactory class provided by spring. net.
Aroundadvice
- Public class aroundadvice: imethodinterceptor
- {
- // Permission system class (can be injected externally)
- Private isecuritymanager manager = new service. securitymanager ();
- Public object invoke (imethodinvocation Invocation)
- {
- // Intercept the Save Method
- If (Invocation. method. Name = "save ")
- {
- Icompanymanager target = (icompanymanager) Invocation. target;
- Return manager. ispass (target. username )? Invocation. Proceed (): NULL;
- }
- Else
- {
- Return invocation. Proceed ();
- }
- }
- }
Copy code
Program
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Icompanymanager target = new companymanager () {Dao = new companydao (), username = "admin "};
- Proxyfactory factory = new proxyfactory (target );
- Factory. addadvice (New aroundadvice ());
- Icompanymanager manager = (icompanymanager) Factory. getproxy ();
- Manager. Save ();
- Console. Readline ();
- }
- }
Copy code
Output: save data
Spring. net uses the class in the system. reflection. emit namespace to dynamically create the Il code at runtime to generate the AOP proxy. This makes the creation of a proxy highly efficient and unrestricted from any hierarchy of inheritance.