. NET: Messages and AOP (i)

Source: Internet
Author: User
Tags aop implement instance method reference require
Method calls and messages

If a target object is delegated to a real proxy instance, then all method calls to the corresponding transparent proxy pass through a process of message conversion and reverse conversion, and
It is precisely when the method call is converted to a message that we have an opportunity to implement the pre (and post) processing, so that the further result is = = "Implementation of so-called AOP."

1. Real agent: Derived from RealProxy, the most important is the replication invoke method. The main function of the Invoke method is to add pre (after) processing to the Intercept method call to
This is to implement AOP.
(1) A real proxy type represents a specific pre (post) processing, if a class wants its own public method to be called outside the appropriate preprocessing or processing,
Then the class can delegate its own instance to a real agent with this pre (POST) processing capability.
(2) The real proxy instance and the transparent proxy instance are one by one corresponding. A class that does not have a transparent proxy, but can get a transparent proxy from a real proxy instance.
(3) Transparent proxy: Converts a method call to a message (the stack frame is actually converted). The method that invokes the target object for the transparent proxy is eventually forwarded to the target object
On At the use level, the transparent proxy can be treated as the target object itself.
(4) Stack Builder Sink: Converts a message to a method call, returns the result of the call to the broker, and then converts the result to a response message via RPC, and then
Converts a response message to a stack after the call, so that the original invocation is convenient for the invocation result.
(5) for different pre (after) processing, you can define different real agent types.

2. If a class (such as Class Example) wants to call its public methods (including ctor), it is done by proxy (the primary purpose is to implement a particular
Pre/post processing), then there are two ways to achieve this:
(1) The example is implemented in the mode of the factory method, so that in its static instance method, a reference to a new instance can be delegated to an implementation-specific preprocessing and
The real agent after processing. The agent's transparent proxy is then coerced into the class reference and returned. In this case, the real agent type only needs to be from RealProxy one
Derived from a class.
(2) Implemented by proxy characteristics (a special real agent).
First, the proxy attribute type must derive from ProxyAttribute, at which point two methods require replication, one is the Invoke method and the other is the CreateInstance method.
U example must be ContextBoundObject derivation, that is, example must bind the object for a context, plus the proxy attribute for the example class.
Note that the context attribute (that is, the proxy attribute) is enforced on classes that are not derived from the ContextBoundObject class.
U in this case, object activation (i.e. new example) is divided into two stages, first calls the real proxy CreateInstance method, generates an uninitialized object;
Then the Invoke method that forwards the ctor to the real agent.

Note: (1) and (2) Comparison: (1) delegating the target object to the agent is example own responsibility, while (2) the responsibility is transferred to the agent.



1. The context attribute modifies a class, and the object is placed in the appropriate context each time the new class produces an object.
2. Context attributes are usually one by one corresponding to context properties.
3. Contextual attributes are added to the context at creation time, or rather, the context attribute checks the current context
Whether there is a property to be added and whether the property value exists is equal to the property value to be added, and if both are satisfied, directly on that
A new instance is created later in this article. Otherwise, the context attribute will require the object ctor to create a new context containing the attributes to be added.
4. A context can contain multiple different context attributes, and each set of contextual property values determines a context.

5. The message receiver itself can serve as any kind of receiver, and the key is to see what the context attribute is scheduled to be
Receivers (e.g., Messenger receiver, server context receiver)

The context property inserts a message sink into the message sink chain in two ways:
(1) Publish public properties only P240
(2) Implement certain types of sink interfaces (such as Messenger sinks), and then confirm a (some) message sink object as the corresponding sink interface type. P349

Note: The context, which exists in the current application domain, is used to convert a property in that context into a method call to the object that the receiver inserts into the context.
The calling context, which exists in the message sink chain, is used to pass information in the chain.



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.