Using message mechanisms to implement. Net AOP (Aspect-Oriented Programming)-basic concepts and Implementations

Source: Internet
Author: User
    1. This seriesArticleThis article describes how to use the. NET remoting message framework to implement AOP.
    2. This series of articles attempts to implement three types of AOP implementation
    3. This series of articles is used as an example.CodeMainly, some concepts and principles will not be discussed too deeply.
    4. I will not talk about the necessity of AOP in actual development, but only at the technical implementation level.
    5. My implementation may not be mature. Please advise me if I have practical application experience.
    6. There are a large number of AOP articles on the network. The purpose of this series of articles is to simply implement the sample code for learning purposes.
    7. All the frameworks implemented in this series aim to intercept data through Attribute Insertion.
    8. This series of code seems simple, but it is actually implemented only after I have studied remoting, because I didn't know much about remoting, so please do not throw bricks

I. Basic Concepts

1. message: the message is a. Net remoting application.ProgramData and the basic unit for passing stack calls. Messages can be used to convert stack calls into easy-to-use. Net objects, so that we can manage various function calls in the program just like other. Net objects. Messages are derived from the iMessage interface. The iMessage interface has an idictionary attribute named properties to save various messages. The iMessage inheritance class encapsulates properties based on their own characteristics for ease of use, for example, imethodreturnmessage has the returnvalue attribute to access the returned results. IMessage subclasses include imethodmessage, imethodreturnmessage, imethodcallmessage, iconstructioncallmessage, iconstructionreturnmessage, and returnmessage. Let's look at their names to find out what they are all about.

2. Proxy: proxy is divided into two categories: transparent proxy (transparentproxy) and real proxy (realproxy). Transparent proxy is not controlled by us, and real proxy can be used to control messages in custom code. A transparent proxy is used to intercept the call of an object method and pass the call to its actual proxy.

3. Export albyrefobject and contextboundobject. To intercept the method call of an object, it must be a class inherited from these two classes. Marshalbyrefobject tells the compiler not to optimize simple functions in this class into Inline code, so that all method calls can be intercepted. The contextboundobject object is bound to its own context. In this way, when the object is initialized, Context Check, creation, and binding will be performed. This also gives us the opportunity to intervene in the object's constructor.

4. Message receiving chain of the context-bound object: The message calling of the context-bound object has a series of message receivers. At first, the message sending group proxy is called (contextboundobject has a real remotingproxy by default ), then the receiver chain is sent to the Special Envoy, and envoyterminatorsink sends the message to the client context receiving chain. Then, clientcontextterminatorsink forwards the message from the client to the server (target object), and the server context receiver chain receives the message, always pass to servercontextterminatorsink to send to the server object receiver chain, and finally objectterminatorsink sends the message to stackbuildersink. (The operation and caller of the four sinks are related to the location of the called party and the interaction between the four sinks. If you want to know more about them, you can write code for one-to-one testing .)

Ii. Three implementations

Implement. Net AOP (Aspect-Oriented Programming) using message mechanism -- Implement through realproxy

Implement. Net AOP (Aspect-Oriented Programming) using message mechanism -- Implement Multi-interception using realproxy and Message Receiver

Implement. Net AOP Using message mechanism (Aspect-Oriented Programming) -- Implement Multi-interception using servercontextsink

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.