Spring. Net study note 12-Aspect-oriented programming (basic)

Source: Internet
Author: User

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:

  1. Public class companydao
  2. {
  3. Public void save ()
  4. {
  5. Console. writeline ("save data ");
  6. }
  7. }
  8. Public interface icompanymanager
  9. {
  10. String username {Get; set ;}
  11. Void save ();
  12. }
  13. Public interface isecuritymanager
  14. {
  15. Bool ispass (string username );
  16. }

Copy code

Securitymanager

  1. Public class securitymanager: isecuritymanager
  2. {
  3. /** // <Summary>
  4. /// Determine the permission
  5. /// </Summary>
  6. /// <Param name = "username"> </param>
  7. /// <Returns> </returns>
  8. Public bool ispass (string username)
  9. {
  10. Return username = "admin ";
  11. }
  12. }

Copy code

The first method is to call isecuritymanager {
Tagshow (Event)
} "> The ispass method of the interface determines the permission.

Simplecompanymanager

  1. Public class simplecompanymanager: icompanymanager
  2. {
  3. Attributes that can be injected externally # attributes that can be injected externally by region
  4. Public String username {Get; set ;}
  5. Public companydao Dao {Get; set ;}
  6. # Endregion
  7. Public void save ()
  8. {
  9. // Determine the permission
  10. Isecuritymanager SECURITY = new securitymanager ();
  11. If (Security. ispass (username ))
  12. {
  13. // Execute the Business Method
  14. //.
  15. // Call the DaO Layer Method
  16. Dao. Save ();
  17. }
  18. Else
  19. {
  20. // Execute other business methods
  21. Console. writeline ("you do not have this permission ");
  22. }
  23. }
  24. }

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

  1. Public class companymanager: icompanymanager
  2. {
  3. Attributes that can be injected externally # attributes that can be injected externally by region
  4. Public String username {Get; set ;}
  5. Public companydao Dao {Get; set ;}
  6. # Endregion
  7. Public void save ()
  8. {
  9. // Execute the Business Method
  10. //.
  11. // Call the DaO Layer Method
  12. Dao. Save ();
  13. }
  14. }

Copy code

Companyproxymanager

  1. Public class companyproxymanager: icompanymanager
  2. {
  3. Public String username {Get; set ;}
  4. Private icompanymanager target = new companymanager ();
  5. Public void save ()
  6. {
  7. // Determine the permission
  8. Isecuritymanager SECURITY = new securitymanager ();
  9. If (Security. ispass (username ))
  10. {
  11. // Call the Save method of the target object
  12. Target. Save ();
  13. }
  14. Else
  15. {
  16. Console. writeline ("you do not have this permission ");
  17. }
  18. }
  19. }

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

  1. Public class aroundadvice: imethodinterceptor
  2. {
  3. // Permission system class (can be injected externally)
  4. Private isecuritymanager manager = new service. securitymanager ();
  5. Public object invoke (imethodinvocation Invocation)
  6. {
  7. // Intercept the Save Method
  8. If (Invocation. method. Name = "save ")
  9. {
  10. Icompanymanager target = (icompanymanager) Invocation. target;
  11. Return manager. ispass (target. username )? Invocation. Proceed (): NULL;
  12. }
  13. Else
  14. {
  15. Return invocation. Proceed ();
  16. }
  17. }
  18. }

Copy code

Program

  1. Class Program
  2. {
  3. Static void main (string [] ARGs)
  4. {
  5. Icompanymanager target = new companymanager () {Dao = new companydao (), username = "admin "};
  6. Proxyfactory factory = new proxyfactory (target );
  7. Factory. addadvice (New aroundadvice ());
  8. Icompanymanager manager = (icompanymanager) Factory. getproxy ();
  9. Manager. Save ();
  10. Console. Readline ();
  11. }
  12. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.