.NET: Message and AOP

xiaoxiao2021-03-06  95

I. Basic knowledge points

Method call and message

If a target object is delegated to a real agent instance, all the method calls for the corresponding transparent agent will pass through a message conversion and the reverse transformation process, but it is the opportunity to achieve pre-predecessors. After the treatment, such a further result is == "Implement a so-called AOP.

1. Real agent: derived from RealProxy, the most important thing is to copy the Invoke method. The main function of the Invoke method is to add pre-(post) processing to intercept method calls to implement AOP. (1) A real agent type represents a specific pre-(post) process, if a class wants your own public approach to proper pre-processing or post-processing, then this class can delegate its own instance Give a real agent for this pre-(then) processing function. (2) The real agent instance is one or one in response to the transparent agent instance. There is no class of transparent agent, but it can be transparent from the real agent instance. (3) Transparent agent: Convert method call to messages (actually converted is a stack frame). The method of calling the target object for a transparent agent will be forwarded to the target object. On the use level, the transparent agent can be treated as the target object itself. (4) Stack Builder Sink: Transform the message into method calls, and returns the result to the agent, then transfer the result to the response message via the RPC, then convert the response message to the stack after the call, so that it is convenient You can get the call results. (5) Different real agent types can be defined for different pre-(post) processing.

2. If a class (such as class example) wants the outside world to call its public method (including CTOR), it is done by the agent (the main purpose is to implement some specific pre / post-processing), then there are two ways to implement: (1) ) Implementing Example in a factory method, so in its Static Instance method, a new instance can be delegated to a real agent that implements a particular pre-processing and post-processing. Then, the agent's transparent agent is then converted to this class reference and returns it. In this case, the real agent type is only derived from RealProxy. (2) Implement by proxy characteristics (a special real agent). First, the agent characteristic type must be derived from ProxyAttribute. At this time, there are two ways to be replied, one is an Invoke method, and the other is the CreateInstance method. Example must be derived from the ContextBoundObject, which must be a context binding object, in addition, adding agent characteristics for the Example class. In this case, the object activation (i.e., when New Example) is divided into two phases, first call the real agent's CreateInstance method, generate an uninited object; then forward the CTOR to the true agent. Note that the contextual characteristics (ie, proxy characteristics) are not implemented from the class of the CONTEXTBOUNDOBJECT class, and interception cannot be achieved.

(1) and (2) Comparison: (1) Entrust the target object to the agent is an Example of its own responsibilities, and the responsibilities are transferred to the agent in (2). other instructions:

1. Context feature is modified, and the object is placed in the corresponding context when the object is generated each time the NEW is generated. 2. The contextual characteristics and context properties are usually one or one. 3. Context Features Add Context Attributes to the context when creating, more specifically, this, the contextual feature checks if there is a presentation to be added in the current context, and the attribute value exists is equal to whether the value to be added, If both are satisfied, create a new instance directly in this context. Otherwise, the contextual feature requires the object CTOR to create a new context containing the properties you want. 4. A context can contain multiple different context properties, each set of determined context attribute values ​​to determine a context.

5. The message receiver itself can be used as any kind of receiver. The key is to see what the context property will arrange it as a receiver (eg, the letter makes the receiver, the server context receiver)

6. Context is a method calling process that exists in the current application domain for converting attributes in the context to a receiver to insert into the object in the context. The call context is present in the message receiver chain, which is used to deliver information in the chain.

2. Procedure

using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Services; using System.Runtime.Remoting.Activation; using System.Runtime.Remoting.Proxies; using System.Runtime.Remoting.Messaging;

/ * This program describes how the interception is performed and the relationship between the real agent and the transparent agent. * / namespace intercept_example {class class1 {[stathread] static void main (String [] args) {example ("sky"); exa.say_hello ();}} // Define a real agent for implementation a special preprocessing and postprocessing public class InterceptProxy: RealProxy {private readonly MarshalByRefObject target; public InterceptProxy (MarshalByRefObject obj, type type): base (type) {this.target = obj;}

public override IMessage Invoke (IMessage msg) {IMethodCallMessage call = (IMethodCallMessage) msg; // constructor is triggered if, at this time has not yet started to build IConstructionCallMessage ctor = call as IConstructionCallMessage target of; if (! ctor = null) {System .Console.writeline ("Forwarding Construction Function Call!");

RealProxy default_proxy = RemotingServices.GetRealProxy (this.target); default_proxy.InitializeServerObject (ctor); MarshalByRefObject tp = (MarshalByRefObject) this.GetTransparentProxy ();

Return EnterpriseServicesHelper.createConstructionReturnMessage (CTOR, TP);

System.Console.WriteLine ( "pretreatment ......"); System.Threading.Thread.Sleep (5000); IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage (this.target, call); System.Console.WriteLine ( "Post-processing ..."); system.threading.thread.sleep (5000); system.console.writeline ("After the end of processing!");

RETURN RESULT_MSG;

}

// definition of a feature that may be above the real use of the agent of the class link characteristics [AttributeUsage (AttributeTargets.Class)] public class InterceptProxyAttribute: ProxyAttribute {// get a transparent proxy public override MarshalByRefObject CreateInstance (Type serverType) { // Uninterened instance MarshalByrefObject target = base.createInstance (Servertype);

InterceptProxy rp = new interceptproxy (target, servertype);

RP.GetTransparentProxy ();}}

[InterceptProxy] public class example: contextBoundObject // Put into a specific context, which will receive the transparent agent of the object {prhenticate (string a) {this.name = a;

Public void say_hello () {console.writeline ("Hello!" Name);}}

/ * * (1) When the call party and the adjacent are located in different contexts, the calling party gets the transparent agent of the subject. The Transparent Agent will pack the method to pack the iMessage object and pass it to the actual agent's INVOKE method. * The default implementation of the INVOKE method is only to pass the IMessage object to the back channel (such as a stack generator, etc.). We can customize your own real agents, rewrite the Invoke method, plus pretreatment and post-processing. * (2) Associate the real agent with the target class with the target class with the target class (such as InterceptProxyAttribute) (inherited from Proxyattribute). This proxy feature mainly rewrites the CREATEINSTANCE * CREATEINSTANCE method of ProxyAttribute, in which the real agent (such as interceptptProxy) is wrapped in the peripheral package of the target object, and returns the transparent agent of the real agent. * (3) In the program, the method of calling the target object directly through the real agent, because the real agent handles the message object (IMESSAGE), which is based on the news world. * (4) Only the reference to the transparent agent can be added to interception (pre-processing and post-processing). * (5) Delicate the class of ContextBoundObject, we can only get its transparent agent; derived from the object of the MashalByrefObject class, we may get its transparent agent or direct reference, depending on whether the object being adjusted and the target object Located in the same context; we don't get its direct references from the objects that are derived from these two classes. * /}

转载请注明原文地址:https://www.9cbs.com/read-123249.html

New Post(0)