Use. NET proxy mode to do class enhancements

Source: Internet
Author: User
Tags exception handling

We have implemented the user registration function and now want to increase the logging function. In particular, before and after the user registration, output a log respectively. We can of course modify the original business code.

Now ask two questions from another angle:
1. Team development, we are likely to not get the source code, how to increase this function.
2. This demand is to increase the log, and then add other requirements (such as exception handling), is still to change the business class it.

To sum up:
We want to do the enhancements of the class without modifying the original business code. Our design should conform to the object-oriented principle: open to expansion, closed to modification .

There are ways to do that. We try the following methods: use the adorner pattern to do the enhanced use of the class . NET proxy mode to do class enhancements use Castle to do class enhancements use Unity to do class enhancements (continued) use AUTOFAC to do class enhancements Original Business Class

Business model

Namespace Testaopbydecorator
{public
    class User
    {public
        string Name {get; set;}
        public int Id {get; set;}}}

Interface design

Namespace Testaopbydecorator
{public
    interface iuserprocessor
    {
        void RegisterUser (user user);
    }
}

Business implementation

Using System;

Namespace Testaopbydecorator
{public
    class Userprocessor:iuserprocessor
    {public
        void RegisterUser (User user)
        {
            if (user = null)
            {return
                ;
            }

            Console.WriteLine (String. Format ("registered with a user {0}:{1}", user.) Id, user. Name);}
        }

Upper Call

Using System;

Namespace Testaopbydecorator
{
    class program
    {
        private static user user = new User {Id = 1, Name = "Yunnan Red "};

        static void Main (string[] args)
        {
            Register ();
            Console.readkey ();
        }

        private static void Register ()
        {
            Iuserprocessor processor = new Userprocessor ();
            Processor. RegisterUser (user);}}
use. NET proxy mode to do class enhancements

We will use the System.Runtime.Remoting.Proxies.RealProxy provided by. NET to do business enhancements to the existing classes.

Using System;
Using System.Runtime.Remoting.Messaging;

Using System.Runtime.Remoting.Proxies; Namespace Testaopbyproxy {public class Userprocessorproxy:realproxy {private Iuserprocessor _processor

        ; Public Userprocessorproxy (Iuserprocessor processor): Base (typeof (Iuserprocessor)) {this._processor =
        Processor public override IMessage Invoke (IMessage msg) {IMethodCallMessage Callmessage = (IMETHODCA
            Llmessage) msg;

            User user = Callmessage.args[0] as user;
            Before (user);
            Object returnvalue = (imethodreturnmessage) callMessage.MethodBase.Invoke (This._processor, Callmessage.args);

            After (user);
        return new ReturnMessage (returnvalue, new object[0], 0, NULL, callmessage); } private void after (user) {Console.WriteLine ("Registered Users:") + user.
        Name);
       } private void before (user user) {     Console.WriteLine ("Pre-registered User:") + user.
        Name);

 }
    }
}

Upper Call

Using System;

Namespace Testaopbyproxy
{
    class program
    {
        private static user user = new User {Id = 1, Name = "Dianhong"};
  static void Main (string[] args)
        {
            registerandlog ();
            Console.readkey ();
        }

        private static void Registerandlog ()
        {
            Iuserprocessor processor = new Userprocessor ();
            Iuserprocessor proxy = (iuserprocessor) new Userprocessorproxy (processor). Gettransparentproxy ()//Agent of the original business class
            proxy. RegisterUser (user)//compliance Interface Contract}}


Compare the business presentation before and after the expansion

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.