. NET proxy Mode

Source: Internet
Author: User

When the objects we need to use are complex or need to be constructed for a long time, we can use the Proxy mode ). For example, if building an object takes a lot of time and computer resources, the Proxy mode allows us to control this situation until we need to use the actual object. A Proxy usually contains the same method as the object to be used. Once this object is used, these methods will be passed to the actual object through Proxy.

Some situations where Proxy can be used:

1. An object, such as a large image, needs to be loaded for a long time.

2. A calculation result that takes a long time to complete, and the intermediate result must be displayed during the calculation process.

3. It takes a long time to load a remote object on a remote computer, especially during peak network transmission.

4. An object has only limited access permissions. The Proxy mode can verify the user's permissions.

Proxy mode can also be used to differentiate the request and actual access of an object instance. For example, multiple objects can be created during program initialization, but not all objects can be used immediately, proxy mode can load the desired real object.

This is a program that needs to load and display a large image. When the program starts, you must determine the image to be displayed, however, the actual image can only be displayed after it is fully loaded! Then we can use Proxy ).

This Proxy mode can delay loading of the actual image until it receives a painting request. During actual image loading, We can pre-load a small, simple image at the position where the actual image is to be displayed through the Proxy mode.

 

What is a competent agent? Obviously, it not only needs to provide complete services to customers, but also needs to effectively control such services and facilitate customers' use. In the design pattern, the proxy pattern officially acts as the proxy, so how can this proxy be implemented in. NET? See the following.

Proxy mode is one of the three GOF23 design modes:Provides a proxy for other objects to control access to this object., UML class diagram:

Figure 1

From this figure, we may not see the intention of the proxy mode to express the true meaning, so we are looking at a picture, as shown below:

Figure 2

Therefore, it is difficult for us to understand the intention of the proxy mode. In the figure, the aClient is the customer who wants to access the aRealSubject object. However, for some reason, we need to control the access. Therefore, we introduced the proxy aProxy, which provides a virtual aRealSubject for aClient access and can control such access. For aClient, this virtual object is exactly the same as the real object.

We have understood what the proxy mode is, so how can we implement it in. NET? As shown in figure 1, you may soon write a sample code, which is implemented in <C # design pattern>. However, the sample code does not solve our actual problems. We do not need to pay attention to every detail in the UML diagram, and do not necessarily need to follow the structure class in the UML diagram. In actual. NET development, how does one implement the proxy mode? Fortunately, Microsoft has provided us with a basic framework for implementing the proxy mode in. NET, and we can directly use it in the Code with slight changes.

In. NET, classes related to implementing the proxy mode include the following:

  1. ContextBoundObject: Defines the base classes of all context-specific classes;
  2. RealProxy: Provides basic proxy functions;
  3. ProxyAttribute: Indicates that the object type requires a custom proxy;

If we want to define a class to be proxy, we only need to inherit from ContextBoundObject, as shown below:

1 [ProxyTag]
2 publicclass RealClass : ContextBoundObject {
3     public RealClass() {
4         Console.WriteLine("Construct a RealClass! ");
5     }
6 }

To define a proxy for RealClass, You need to inherit your proxy class from RealProxy and reload the Invoke method:

01 public override IMessage Invoke(IMessage msg) {
02     // Process the custom constructor object
03     PreProcess(msg);
04     IMessage retMsg=null;
05     if (msg is IConstructionCallMessage) {
06         IConstructionCallMessage ccm = (IConstructionCallMessage)msg;
07  
08         retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ccm, (MarshalByRefObject)this.GetTransparentProxy());
09     }
10     // Process custom constructor objects
11     PostProcess(retMsg);
12     return retMsg;
13 }

After defining the proxy class, you need to define the Attribute associated with the two to complete the required proxy. In this case, you need to inherit your own label from ProxyAttribute and reload the CreateInstance method:

01 public override MarshalByRefObject CreateInstance(Type serverType) {
02     MarshalByRefObject mobj = base.CreateInstance(serverType);
03     if (aspectManaged) {
04         RealProxy realProxy = new ProxyClass(serverType, mobj);
05         MarshalByRefObject retobj = realProxy.GetTransparentProxy() as MarshalByRefObject;
06         return retobj;
07     }
08     else {
09         return mobj;
10     }
11 }

After the above steps, we have completed a proxy class that can be used directly. Write a line of test code to verify our example.

1 try {
2     RealClass pc = new RealClass();
3 }
4 catch (Exception ex) {
5     Console.WriteLine(ex.Message);
6 }

The running result is:

According to the definition of the class, when creating a RealClass instance, we will output "construct a RealClass !", The actual test results are as follows. We can see that the proxy RealProxy has achieved our goal.

Note:

1. Read this article to understand the proxy mode. If you have any questions, leave a message.

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.