- This seriesArticleThis article describes how to use the. NET remoting message framework to implement AOP.
- This series of articles attempts to implement three types of AOP implementation
- This series of articles is used as an example.CodeMainly, some concepts and principles will not be discussed too deeply.
- I will not talk about the necessity of AOP in actual development, but only at the technical implementation level.
- My implementation may not be mature. Please advise me if I have practical application experience.
- 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.
- All the frameworks implemented in this series aim to intercept data through Attribute Insertion.
- 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