Self-made MVC framework plugin and Interceptor Basics

Source: Internet
Author: User


In the previous article, I wrote about my own MVC framework, and then I talked about plugins and interceptors!

In dealing with some common logic it is best to encapsulate a plugin or interceptor so that it can be used directly in the future. In my framework, you can implement plug-ins or interceptors by inheriting the following abstract classes.

1. Aspectinterceptor Abstract class

An AOP interceptor that handles dynamic weaving, stephen.view the Interceptor abstract class in the framework, all require that the dynamic interceptor implement this class, the interceptor principle is implemented through ASPECTSHARP. The interceptor is suitable for the control layer interception, and to enable the Controller class to support the dynamic interception must add Dynamicattribute characteristics to the controller

Two ways to implement

Proceed (IDictionary mycontext) when the interception method is processed, a hashtable parameter must be passed

Entry (imethodinvocation invocation) when the interception method has not yet processed the subsequent interception processing, the parameter is the class of the method call (Aspectsharp Framework Class)

Configuration syntax can be found in: http://www.cnblogs.com/netcorner/archive/2011/04/01/2911966.html

Examples of Interceptor implementations:

    [Serializable]    publicclass  Randproductinterceptor:aspectinterceptor    {        publicoverridevoid  Proceed (IDictionary mycontext)        {            ...        }    }

Examples of controller implementations:

namespace jobmate.controllers{    [Dynamic]   // must be declared otherwise the public class cannot be intercepted      Default    {        [mylogin]        publicvirtual IDictionary Index ( IDictionary context)   // The method must be guaranteed as a virtual method, otherwise it cannot intercept         {            return context;        }        ...}}

To configure an interceptor example:

<Configuration>    <configsections>        < Sectionname= "Aspectsharp"type= "AspectSharp.Builder.SectionHandler.AspectSharpConfigurationHandler, Aspectsharp"/>... ..</configsections>        <Aspectsharp>        <Configuration>aspect Processor1 for [Jobmate.controllers] Pointcut Method (* Index (*)) Advice (randproductinterceptor) advice (shareinterceptor) end end</Configuration>    </Aspectsharp>... ..</Configuration>

Intercepts all methods that are named index in the Jobmate.controllers package.

2. Beforehandcommonattribute Abstract class

Statically woven AOP interceptors, stephen.view in the framework of an interceptor abstract class, all require static interceptors to implement this class, while static interceptors must be serializable (class adds Serializable

Feature), the interceptor principle is implemented through POSTSHARP. Interceptors are handled in two ways:

1). The class library must be built with the POSTSHARP software installed, and the Project class Library will include PostSharp.Laos.dll, PostSharp.Public.dll, but post-compilation does not need to be installed or introduced.

2). The interceptor is placed in the Web site App_Code folder and does not need to be postsharp compiled.

The two methods are not the same, the first use of Postsharp code in the compilation of the time of weaving, the other one using the principle of reflection interception, the former more efficient than the latter.

Examples of Interceptor implementations:

    [Serializable]   Declares that the class must be serialized, otherwise it cannot intercept public    class Myloginattribute:beforehandcommonattribute    {public        override void Beforehand (IDictionary mycontext)        {            ...        }    }

Controller example:

Namespace Netcorner.controllers.integration{public class Commend {        [MyLogin (aspectpriority = 1)]        [ Myrolecheck (aspectpriority = 0)] public        virtual new IDictionary managelist (IDictionary context)        {...}}         }

Aspectpriority is the way in which multiple interceptors are prioritized in the Postsharp class, the smaller the value, the higher the execution priority, and the lower the inverse.

It is important to note that this statically-woven interceptor does not have to be placed on the method and can be placed on the class.

    [Sharedata (Attributetargetmembers = @ "regex:^ (?!. *action). *$ ")]    [MyLogin (attributetargetmembers = @" regex:^ (?!. *action). *$ ", aspectpriority = 1)]    [Myrolecheck (aspectpriority = 0)] Public    class administration    {        [ Pagination (aspectpriority = 3, Key = "Jobmate.JM_AM_Employee.Employee")]        [Querydata (aspectpriority = 2)]        Public IDictionary Employeemanage (IDictionary context)        {            ...        }            [GUID (aspectpriority = 2)]            [FormData (aspectpriority = 1)]            [Userlogger]            [Myrolecheck (aspectpriority = 0)]            [Breakromoteurl (aspectpriority =-1)]            Protected Object Newaction (IDictionary context)            {                ...            }    }

The interception method can be filtered in a regular manner by attributetargetmembers. As in the previous example, the Newaction method will be intercepted without intercepting employeemanage

Use visible: http://www.cnblogs.com/netcorner/p/3756585.html

3 Proceedplugin Abstract class

Using the same beforehandcommonattribute, unlike the Beforehandcommonattribute, it is intercepted after the controller method has been executed.

Demo Example:Http://files.cnblogs.com/netcorner/%E7%A4%BA%E4%BE%8B1.rar

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.