Using AUTOFAC 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. This series attempts to implement non-invasive enhancements to the original business class by using the adorner pattern for 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);}}
using AUTOFAC to do class enhancements

We will use the Third-party AUTOFAC to do business enhancements to the original classes, first using NuGet installation.

Business Enhancement Implementation

Using Castle.dynamicproxy;
Using System;

Namespace Testaopbyautofac
{public
    class Userprocessorlog:iinterceptor
    {public
        void Intercept ( Iinvocation invocation)
        {
            User user = invocation. Arguments[0] as User;

            Before (user);
            Invocation. Proceed ();
            After (user);
        }

        private void after (user user)
        {
            Console.WriteLine ("Registered Users:") + user. Name);

        private void before (user user)
        {
            Console.WriteLine (before registering Users:) + user. Name);}}

Upper Call

Using AUTOFAC;
Using Autofac.Extras.DynamicProxy2;

Using System;

        Namespace TESTAOPBYAUTOFAC {class Program {private static user user = new User {Id = 1, Name = "Dianhong"};
            static void Main (string[] args) {registerandlog ();
        Console.readkey ();
            private static void Registerandlog () {Containerbuilder builder = new Containerbuilder (); Builder. Registertype<userprocessor> (). As<iuserprocessor> (). Enableinterfaceinterceptors ().
            Interceptedby (typeof (Userprocessorlog)); Builder.

            Registertype<userprocessorlog> (); var container = Builder.

            Build (); using (var scope = container. Beginlifetimescope ()) {Iuserprocessor processor = (iuserprocessor) scope.

                Resolve<iuserprocessor> (); Processor.
            RegisterUser (user);
     }   }
    }
}

 

Compare the business presentation before and after the expansion

Here we use code to configure interceptors for existing business classes (AOP Dependency injection), or to use attributes.
Using attributes to strengthen business classes

Using Autofac.Extras.DynamicProxy2;
Using System;

Namespace Testaopbyautofac
{
    [Intercept (typeof (Userprocessorlog))] 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 AUTOFAC;
Using Autofac.Extras.DynamicProxy2;

Using System;

        Namespace TESTAOPBYAUTOFAC {class Program {private static user user = new User {Id = 1, Name = "Dianhong"};
            static void Main (string[] args) {Register ();
            Console.WriteLine ("--------------");

            Registerandlog ();
        Console.readkey ();
            private static void Registerandlog () {Containerbuilder builder = new Containerbuilder (); Builder. Registertype<userprocessor> (). As<iuserprocessor> ().
                Enableinterfaceinterceptors (); //. Interceptedby (typeof (Userprocessorlog));//You can use attributes instead of builder.

            Registertype<userprocessorlog> (); var container = Builder.

            Build (); using (var scope = container. Beginlifetimescope ()) {Iuserprocessor processor = (iuserprocessor) scope. Resolve<iuSerprocessor> (); Processor.
            RegisterUser (user);
            The private static void Register () {Iuserprocessor processor = new Userprocessor (); Processor.
        RegisterUser (user);
 }
    }
}

We are using interface injection (enableinterfaceinterceptors), or class injection (enableclassinterceptors), where the method of the existing business class needs to display the declaration as Vitual virtual method. At the top of the call, we can register and parse a class directly, not dependent on the interface.

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.