AOP (the concept of aspect-oriented programming, this article is translation)

Source: Internet
Author: User

What is AOP?

AOP is the abbreviation for Aspect oriented programming. AOP is a continuation of OOP, a hotspot in software development, an important content in the spring framework, and a derivative model of functional programming. AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.

For more information, please Baidu or Google: application of AOP in. Net

Application Scenarios for AOP

The biggest advantage of AOP is that you only need to worry about the aspects in one place, programming once and all the places needed for the application. AOP has many uses, such as:

    • Log (Logging): Implementing Logging in an application
    • Authentication (Identity check): Use actions before authentication (such as allowing some actions to be authenticated only by the user).
    • Data validation (Validation or Notificatio): implements a validation or notification property setter (invokes the PropertyChange event when the property has changed the class, implementing the INotifyPropertyChanged Interface).
    • Behavioral change (changing the behavior): Change the behavior of some methods.

As you can see, AOP has many uses, but you have to use it with care. It will keep some code within your line of sight, but it Echnical's still exists where every invocation of the runtime exists. It can have defects and severely affect the performance of the application. A subtle mistake can cost you a lot of debugging time. If your aspect is echnical 檛 used in many places, sometimes echnical's best to add it directly to the code.

The AOP implementation uses some common tricks:

    • Add preprocessor source code to use, as in C + +.
    • Use the post-processor to add the binary code after the instruction is compiled.
    • Add code when compiling with a special compiler.
    • Use a code at the runtime interceptor to intercept execution and add the required code.

In the. NET Framework, the most commonly used technology is post-processing and code interception. The former uses the technique Postsharp (postsharp.net), which uses a dependency injection (DI) container, such as Castle Dynamicproxy (bit. ly/jze631) and Unity (unity.codeplex.com). These tools (referred to as dependency injection containers) typically use decorator design Patterns or proxies to execute code interception

Advantages and disadvantages of applying AOP

The biggest advantage of AOP is that you only need to worry about the aspects in one place, programming once and all the places needed for the application. AOP has many uses, such as:

    • Implement logging in the application.
    • Use pre-authentication actions (such as allowing some actions to be authenticated only to users).
    • Implement a validation or notification property setter (call the PropertyChange event when the property has changed the class to implement the INotifyPropertyChanged interface).
    • Change the behavior of some methods.

As you can see, AOP is useful, but you have to use it with care. Although AOP code is implemented in a class that is not used, AOP exists in the appropriate place each time the runtime is called. It may be defective and may severely affect the performance of your application. A small plane bug can take too much of your debugging time. If your AOP is at least used at some point, it's a good idea to add it directly to your code.

The AOP implementation uses some common tricks:

    • Add preprocessor source code to use, as in C + +.
    • Use the post-processor to add the binary code after the instruction is compiled.
    • Add code when compiling with a special compiler.
    • Use a code at the runtime interceptor to intercept execution and add the required code.

In. NET Framework, of which the most commonly used techniques are post-processing and code interception. The former is the technique used by Postsharp (postsharp.net), which uses a dependency injection (DI) container, such as the castle Dynamicproxy (bit. ly/jze631) and Unification (unity.codeplex.com). These tools typically use a design pattern called decoration or an agent to execute code interception.

Knowledge Extension = "Decorative design pattern"

The decorative design pattern solves a common problem: you have a class and want to add some functionality. You have several options:

    • You can add new functionality directly to the class. However, this class has more than one responsibility and does not conform to the "single responsibility" principle.
    • You can create a new class that allows this function to invoke the corresponding method of its old class. But this brings up a new question: What happens if you want to use a class that doesn't have new functionality?
    • You can create a new class, let the new class inherit the old class, add new functionality, but this can lead to many new classes. For example, you have a repository class to create, read, update, and delete (CRUD) database operations and you want to add an audit. Then, you want to add data validation to ensure that the data is updated correctly. After that, you may also want to verify access to the classes that are accessible only to authorized users. These are big questions: You can have some classes, implement three aspects, or implement two of them, or even only one of them. How many classes will you have in the end?
    • You can use an outer wrapper to decorate these classes, create a new class with a tangent feature, and then call the old class. This way, if you need an aspect, you wrap it once. Two aspects, you wrap two times, and so on. For example, you order a toy (as we are geeks, Xbox or Smartphone). First, it requires an outermost wrapper with enough space and protection. Then, for the 2nd tier package, you can order a tape run, stripe, card and gift paper box. The 3rd layer of packaging, a polystyrene foam ball with anti-fragile function to wrap it. Now he has three layers of outer packaging, each of which has different functions, and each outer package is independent of each other. You can buy one without gift box, even without any packaging toys (special discount!). You can pack your toys in any combination of your ideas, but they do not change the function of the toy.

AOP Reference

This article translated from: https://msdn.microsoft.com/en-us/magazine/dn574804.aspx (plane-oriented programming)

AOP (the concept of aspect-oriented programming, this article is translation)

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.