. Net-proxy)

Source: Internet
Author: User

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 an exampleCode, <C # design pattern> is implemented in this book. 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:

 
[Proxytag] public class realclass: contextboundobject {public realclass () {console. writeline ("constructs a realclass! ");}}

To define a proxy for realclass, You need to inherit your proxy class from realproxy and reload the invoke method:

Public override iMessage invoke (iMessage MSG) {// process preprocess (MSG) before the custom constructor object; iMessage retmsg = NULL; If (MSG is iconstructioncallmessage) {iconstructioncallmessage CCM = (iconstructioncallmessage) MSG; retmsg = enterpriseserviceshelper. createconstructionreturnmessage (CCM, (marshalbyrefobject) This. gettransparentproxy ();} // process postprocess (retmsg); Return retmsg after the custom object is constructed ;}

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:

Public override implements albyrefobject createinstance (type servertype) {export albyrefobject mobj = base. createinstance (servertype); If (aspectmanaged) {realproxy = new proxyclass (servertype, mobj); financialbyrefobject retobj = realproxy. gettransparentproxy () as your albyrefobject; return retobj;} else {return mobj ;}}

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.

 
Try {realclass Pc = new realclass ();} catch (exception ex) {console. writeline (ex. Message );}

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.

2. If you need a demo, leave an email address (I don't know how to upload the demo ).

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.