AOP Slicing programming

Source: Internet
Author: User

Introduction If you are familiar with aspect-oriented programming (AOP), you will know that adding "facets" to your code can make your code clearer and maintainable. But AOP often relies on third-party class libraries or hard-coded. NET features to work. While the benefits of these implementations outweigh their complexities, I'm still looking for an easier way to implement AOP, to try my code more clearly. I moved them out individually and named ASPECTF. Aspect oriented Programming (AOP) Background "facets" refers to things that have the same commonality in the code you write in different parts of the project. It may be a special way of handling exceptions in your code, recording method calls, time processing, re-executing some methods, and so on. If you do not use any of the class libraries that face-cutting programming to do these things, you will leave some very simple and repetitive code in your entire project that will make your code difficult to maintain. For example, some methods in your business logic layer need to be logged, some exceptions need to be processed, some executions need to be timed, database operations need to be retried, and so on. So, maybe you'll write the following code.
Intercept Messages      Public class Aopattribute:proxyattribute    {        publicoverride  MarshalByRefObject CreateInstance (Type servertype)        {             //Aopproxy is the interception message processing            new  aopproxy (servertype)            ; return  as MarshalByRefObject;        }    }
processing Messages Public classAopproxy:realproxy { Publicaopproxy (Type servertype):Base(servertype) {}//How to handle messages after they are intercepted         Public OverrideIMessage Invoke (IMessage msg) {if(msg isIConstructionCallMessage)//if it is a constructor, return it in the original way. {iconstructioncallmessage constructcallmsg= Msg asIConstructionCallMessage; IConstructionReturnMessage Constructionreturnmessage= This.                Initializeserverobject ((iconstructioncallmessage) msg); Realproxy.setstubdata ( This, Constructionreturnmessage.returnvalue); Console.WriteLine ("Call Constructor"); returnConstructionreturnmessage; }            Else  //if it is a method call (the property is also one of the method calls){imethodcallmessage callmsg= Msg asIMethodCallMessage;                IMessage message; Try                {                    Object[] args =Callmsg.args; //call the method you want to implement here.                    Objecto =CallMsg.MethodBase.Invoke (Getunwrappedserver (), args); Message=Newreturnmessage (o, args, args.                Length, Callmsg.logicalcallcontext, callmsg); }                Catch(Exception e) {message=NewReturnMessage (E, callmsg); } Console.WriteLine (Message. properties["__return"].                ToString ()); returnmessage; }        }        }
called class [Aopattribute] Public classAopclass:contextboundobject { Public stringHello () {return "Welcome"; }         Public ObjectGetName (stringname) {            returnName +"said: This tree is I planted, this road is I drive, want to live from now on, leave the life"; }    }
    class program    {        staticvoid Main (string[] args)        {           Aopclass ap=New  Aopclass ();             int 1 ;            Ap. Hello ();            K+ +;            Ap. GetName ("donsgheng");            Console.WriteLine (k);        }    }

AOP Slicing programming

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.