C # Agent Application: cachable

Source: Internet
Author: User
Tags bool

At ease, this is not to say the design pattern of the agent mode, said is the C # RealProxy usage, mainly used to: by labeling the class, let class do more work, such as to determine whether there is a cache, there is a direct return to the cache object, no save as a cache, Waiting for the next request is a quicker way to get the data (which is, of course, one of the common uses, which is the action of MVC)

Here is the sequence diagram:

. Net Object Generation Interceptor belongs to. NET self behavior, no additional write code is required.

Code consumer refers to an object, such as a console program, or a Web program, that wants to invoke realobject for invocation.

ProxyAttribute defines the specific proxy class is which, this proxy class is their own inheritance RealProxy write a proxy class, this class needs to add a front, back, surround and other methods (specifically according to the requirements)

Let's look at the specifics of how. NET implementation:

public class Followaction {public bool Stopexecute {get; set;}                Used in the predecessor method to indicate whether to stop executing the real method body, for example, if cache value has been found, so there is no need to continue running the method body.  Save cache value Variable} public abstract class Cachablerealproxy:realproxy {private MarshalByRefObject
    
        Target Public Myaoprealproxy (Type objType, MarshalByRefObject obj): Base (objType) {target = obj
        ;
            public override IMessage Invoke (IMessage msg) {IMessage retmsg = null;
            IMethodCallMessage Methodcall = (imethodcallmessage) msg;
            IMethodReturnMessage Methodreturn = null;
            object[] Copiedargs = Array.CreateInstance (typeof (Object), MethodCall.Args.Length) as object[];
            MethodCall.Args.CopyTo (Copiedargs, 0); if (msg is iconstructioncallmessage) {iconstructioncallmessage CCM = (IconstrucTioncallmessage) msg; Remotingservices.getrealproxy (target).
                Initializeserverobject (CCM);
                ObjRef oref = Remotingservices.marshal (target);
                Remotingservices.unmarshal (OREF); Retmsg = Enterpriseserviceshelper.createconstructionreturnmessage (CCM, (MarshalByRefObject) this.
    
            Gettransparentproxy ());
                else {bool aopattributeexists = false;
                object[] Attrs = MethodCall.MethodBase.GetCustomAttributes (false); if (attrs!= null && attrs.
                        Count () > 0) {foreach (Object o in Attrs) {
                        Cachableattribute attr = o as Cachableattribute;
                            if (attr!= null) {aopattributeexists = true;
                        Break
             }
                    }
                }   Followaction Follow=null; if (aopattributeexists) follow = this.
    
                Preprocess (msg);
                    try {Object returnvalue = null; if (Follow!= null && follow. Stopexecute) returnvalue = follow.
                    result;
                    else returnvalue = MethodCall.MethodBase.Invoke (This.target, Copiedargs); Methodreturn = new ReturnMessage (returnvalue, Copiedargs, Copiedargs.length, Methodcall.logicalcallcontext,
    
                    Methodcall); if (follow = null | |!follow. Stopexecute) if (aopattributeexists) this.
                PostProcess (msg, methodreturn); The catch (Exception ex) {if (null!= ex. innerexception) {Methodreturn = new ReturnMessage (ex. InnerException, MethoDcall); 
                    else {Methodreturn = new ReturnMessage (ex, Methodcall);
    
            } retmsg = Methodreturn;
        return retmsg;
        } public override Followaction Preprocess (System.Runtime.Remoting.Messaging.IMessage requestmsg)/handling predecessor method
            {bool Cachedefinationtagexists = true;
            Cachableattribute cachedefine = Checkcachedefinationtag (requestmsg, ref cachedefinationtagexists);
    
            if (!cachedefinationtagexists) return null;
            String cachekey = Cachedefine.generatecachekey (); Object O=cachemanager.instance (). Getcachecore ().
            Get (Cachedefine.location, CacheKey);
                if (o!= null) {followaction follow = new Followaction (); Follow.
                result = O; Follow.
             Stopexecute = true;   return follow;
        return null; } public override void PostProcess (System.Runtime.Remoting.Messaging.IMessage requestmsg, S Ystem.
            Runtime.Remoting.Messaging.IMessage responsemsg)//Processing post Method {bool cachedefinationtagexists = true;
            Cachableattribute cachedefine = Checkcachedefinationtag (requestmsg, ref cachedefinationtagexists);
    
            if (!cachedefinationtagexists) return;
    
            ReturnMessage returnmsg = (returnmessage) responsemsg;
            String cachekey = Cachedefine.generatecachekey (); Cachemanager.instance (). Getcachecore ().
        Set (Cachedefine.location, CacheKey, Returnmsg.returnvalue); private static Cachableattribute Checkcachedefinationtag (System.Runtime.Remoting.Messaging.IMessage requestmsg, ref bool Cachedefinationtagexists)//help function {IMethodCallMessage Methodcall = (imethodcallmessage) requestmsg;
            object[] Attrs = MethodCall.MethodBase.GetCustomAttributes (typeof (Cachableattribute), false); if (attrs = null | | attrs.
            Count () <= 0) cachedefinationtagexists = false;
            Cachableattribute cachedefine = attrs[0] as Cachableattribute;
            if (Cachedefine = = null) Cachedefinationtagexists = FALSE;
        return cachedefine; }
    }

Related Article

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.