自製MVC架構的外掛程式與攔截器基礎

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   


  上篇談到我自己寫的MVC架構,接下來講講外掛程式及攔截器!  

  在處理一些通用的邏輯最好把它封裝一個外掛程式或者攔截器,以便日後可以直接拿過來直接使用。在我的架構中可以通過繼承以下抽象類別來實現外掛程式或者攔截器。

1. AspectInterceptor抽象類別

  處理動態織入的AOP攔截器,Stephen.View架構中的攔截器抽象類別,所有需要動態攔截器必須實現該類,該攔截器原理是通過aspectsharp實現的。該攔截器適用於控制層的攔截,且要使控制器類支援動態攔截必須給控制器加上DynamicAttribute特性

實現的兩個方法

Proceed(IDictionary myContext) 當攔截的方法處理完以後進行的攔截處理,必須有一個hashtable參數傳遞

Entry(IMethodInvocation invocation) 當攔截的方法還未處理以後進行的攔截處理,參數是方法調用的類(aspectsharp架構類)

配置文法可參見:http://www.cnblogs.com/netcorner/archive/2011/04/01/2911966.html

攔截器實現樣本:

    [Serializable]    public class RandProductInterceptor : AspectInterceptor    {        public override void Proceed(IDictionary myContext)        {            …        }    }

控制器實現樣本:

namespace Jobmate.Controllers{    [Dynamic]   //必須聲明,否則無法攔截    public class Default    {        [MyLogin]        public virtual IDictionary Index(IDictionary context)   //該方法必須保證為虛方法,否則不能攔截        {            return context;        }        …}}

配置攔截器樣本:

<configuration>    <configSections>        <section name="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>

對Jobmate.Controllers包中所有名為Index的方法進行攔截處理.

2. BeforehandCommonAttribute抽象類別

  靜態織入的AOP攔截器,Stephen.View架構中的攔截器抽象類別,所有需要靜態攔截器必須實現該類,同時靜態攔截器必須可被序列化(類上加Serializable

特性),該攔截器原理是通過postsharp實現的.攔截器有兩種處理方式:

1).類庫產生時必須安裝postsharp軟體, 且項目類庫中加入PostSharp.Laos.dll、PostSharp.Public.dll,但編譯之後發布就不需要安裝或引入了。

2).攔截器放在網站網站app_code檔案夾中是不需要postsharp編譯的。

兩者方式是不一樣的,第一種採用postsharp代碼在編譯的時候織入,另外一種採用反射原理攔截,前者的效率要高於後者。

攔截器實現樣本:

    [Serializable]   //聲明類必須被序列化,否則無法攔截    public class MyLoginAttribute : BeforehandCommonAttribute    {        public override void Beforehand(IDictionary myContext)        {            …        }    }

控制器樣本:

namespace Netcorner.Controllers.integration{public class Commend {        [MyLogin(AspectPriority = 1)]        [MyRoleCheck(AspectPriority = 0)]        public virtual new IDictionary ManageList(IDictionary context)        {…         }}}

AspectPriority是postsharp類中對多個攔截器進行優先順序設定的方式,值越小說明執行優先順序越高,反之者越低。

值得注意的是,這個靜態織入的攔截器不一定只能放在方法上的特性,還可以放在類上。

    [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)            {                …            }    }

可通過AttributeTargetMembers發生以正則的方式篩選可攔截的方法。如上例中將攔截NewAction方法,而不攔截EmployeeManage

使用可見:http://www.cnblogs.com/netcorner/p/3756585.html

3 ProceedPlugin抽象類別

 

使用同BeforehandCommonAttribute,與BeforehandCommonAttribute不一樣的地方是它是在控制器方法執行完以後再攔截的。

示範樣本:http://files.cnblogs.com/netcorner/%E7%A4%BA%E4%BE%8B1.rar

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.