亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

[渣譯文]Ninject:Dependency injection for fil

系統 2193 0

咱鳥語很差,文采為0,重在概述,請多包涵...

使用新版的Ninject MVC3時發現過濾器的DI跟原來(kernel.BindFilter)不一樣了,某度上沒找到中文的,遂摸到Ninject老家找到相關文檔,看了半天,干脆在這里留個底,方便自己將來回味,也給后來人指個路...寫到一半突發奇想,既然文言文咱寫不好,干脆寫個口語賣萌的吧.....想看渣文言文的同學請反白...

環境:MVC3 + Ninject 3.0.1.10 + Ninject.MVC3 3.0.0.6?

Dependency injection for filters

依賴注入到MVC過濾器

MVC3 introduced a completely new pattern to configure filters for controllers and its actions. While injection of filter attributes is still supported it is recommended using this new pattern for filter configuration because it has the advantage to support constructor injection and does not require the InjectAttribute anymore.

MVC3使用了全新的模式來配置控制器和動作上的過濾器。雖然過濾器特性注入依然被支持,但是我們建議使用新模式來配置過濾器。因為它具有支持構造函數注入的優勢,不再需要注入特性了。

新的N-MVC3用了新配置模式啦,原來的不管用鳥,新配置模式支持構造函數注入,無需注入特性鳥...

First of all you have to create your filter class by implementing one of the filter interfaces e.g. IActionFilter. All the dependencies are added to the constructor. The following example of a logging filter has a logger as dependency and can be configured with the level that is used to log.

首先,你需要建立一個實現了過濾器接口(如IActionFilter)的過濾器類,將依賴項添加到構造函數中。下面的例子實現了一個日志過濾器,其中包含一個Log依賴項,以及用于記錄日志的級別。

建一個實現了過濾器接口的過濾器類,給你想注入的東東扔到構造函數里面,這個例子中有日志及級別依賴。

      
         1
      
      
        public
      
      
        class
      
      
         LogFilter : IActionFilter


      
      
         2
      
      
        {


      
      
         3
      
      
        private
      
      
        readonly
      
      
         ILog log;


      
      
         4
      
      
        private
      
      
        readonly
      
      
         Level logLevel;


      
      
         5
      
      
         6
      
      
        public
      
      
         LogFilter(ILog log, Level logLevel)


      
      
         7
      
      
            {


      
      
         8
      
      
        this
      
      .log =
      
         log;


      
      
         9
      
      
        this
      
      .logLevel =
      
         logLevel;


      
      
        10
      
      
            }


      
      
        11
      
      
        12
      
      
        public
      
      
        void
      
      
         OnActionExecuting(ActionExecutingContext filterContext)


      
      
        13
      
      
            {


      
      
        14
      
      
        var
      
       message = 
      
        string
      
      
        .Format(


      
      
        15
      
      
                        CultureInfo.InvariantCulture,


      
      
        16
      
      
        "
      
      
        Executing action {0}.{1}
      
      
        "
      
      
        ,


      
      
        17
      
      
                        filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,


      
      
        18
      
      
                        filterContext.ActionDescriptor.ActionName),


      
      
        19
      
      
        this
      
      .log.Logger.Log(
      
        typeof
      
      (LogFilter), 
      
        this
      
      .logLevel, message, 
      
        null
      
      
        );


      
      
        20
      
      
            }


      
      
        21
      
      
        22
      
      
        public
      
      
        void
      
      
         OnActionExecuted(ActionExecutedContext filterContext)


      
      
        23
      
      
            {


      
      
        24
      
      
        var
      
       message = 
      
        string
      
      
        .Format(


      
      
        25
      
      
                        CultureInfo.InvariantCulture,


      
      
        26
      
      
        "
      
      
        Executed action {0}.{1}
      
      
        "
      
      
        ,


      
      
        27
      
      
                        filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,


      
      
        28
      
      
                        filterContext.ActionDescriptor.ActionName),


      
      
        29
      
      
        this
      
      .log.Logger.Log(
      
        typeof
      
      (LogFilter), 
      
        this
      
      .logLevel, message, 
      
        null
      
      
        );


      
      
        30
      
      
            }


      
      
        31
      
       }
    

嗯,C#用戶應該都能看懂這個過濾器想干嘛...

To apply this filter to an action or controller we need to specify a binding. But unlike other bindings, filters require that BindFilter is used instead of the normal Bind. Additionally, the type of the filter you have to specify the filter scope and the filter order. More information about these two parameters can be found "Brad Wilsons Blog": http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html .

In the example below the LogFilter is applied to every action and configured with the log level info.

要應用這個過濾器到控制器或動作,我們需要指定一個綁定。但是不像其他的綁定,舊的BindFilter 綁定方法被換為Bind方法。另外,你必須指定過濾器的作用域及過濾順序。關于這兩個參數的更多信息,請參見Brad Wilsons的博客。

下面的例子中,日志過濾器被應用到控制器上的每個動作中并配置日志級別信息。(構造器注入)

想給這個過濾器弄控制器上面去,舊的BindFiler方法不管用了,需要模塊綁定啦。綁定的時候你得指定著過濾器的作用域跟執行順序,想看這哥倆更多信息的,請猛擊布拉德威爾遜老濕的博客文(見上)。

下面代碼中,日志過濾器被注冊到每一個控制器上,同時配置了日志級別信息。(構造器注入)

      
         1
      
      
        public
      
      
        class
      
      
         LoggingModule : NinjectModule


      
      
         2
      
      
        {


      
      
         3
      
      
        public
      
      
        override
      
      
        void
      
      
         Load()


      
      
         4
      
      
            {


      
      
         5
      
      
        this
      
      .Bind<ILog>
      
        ().ToMethod(GetLogger);


      
      
         6
      
      
        this
      
      .BindFilter<LogFilter>(FilterScope.Controller, 
      
        0
      
      
        )


      
      
         7
      
                   .WithConstructorArgument(
      
        "
      
      
        logLevel
      
      
        "
      
      
        , Level.Info);


      
      
         8
      
      
            }


      
      
         9
      
      
        10
      
      
        private
      
      
        static
      
      
         ILog GetLogger(IContext ctx)


      
      
        11
      
      
            {


      
      
        12
      
      
        var
      
       filterContext =
      
         ctx.Request.ParentRequest.Parameters


      
      
        13
      
                             .OfType<FilterContextParameter>
      
        ().SingleOrDefault();


      
      
        14
      
      
        return
      
       LogManager.GetLogger(filterContext == 
      
        null
      
       ?


      
        15
      
      
                    ctx.Request.Target.Member.DeclaringType :


      
      
        16
      
      
                    filterContext.ActionDescriptor.ControllerDescriptor.ControllerType);


      
      
        17
      
      
            }


      
      
        18
      
       }
    

Further Information on this topic:

更多信息請參見:

過濾器條件綁定

過濾器配置

更多相關信息請猛擊:過濾器條件綁定,過濾器配置(見上)

原文:

Dependency injection for filters

補:找不到BindFilter的同學,請using Ninject.Web.Mvc.FilterBindingSyntax;

[渣譯文]Ninject:Dependency injection for filters


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产成人精品日本亚洲专一区 | 一本到在线观看视频不卡 | 在线观看香蕉免费啪在线观看 | 色天天色综合 | 国产精品久久久久亚洲 | 欧美高清不卡午夜精品免费视频 | 欧美日韩亚洲国产精品一区二区 | 黄色视屏在线免费观看 | 午夜精品国产 | 久久精品免费i 国产 | 亚洲精品久久久久中文字小说 | 免费视频99 | 91在线视频在线 | 香蕉久久夜色精品国产小说 | 欧美做爰xxxⅹ在线视频hd | 久久国产在线视频 | 国产精品久久久久久久y | 成年人性生活免费视频 | 久久国产精品久久国产精品 | 2021国内精品久久久久影院 | 久久精品综合免费观看 | 麻豆久久婷婷国产综合五月 | 国产精品中文字幕在线观看 | 欧美激情亚洲精品日韩1区2区 | 拍拍拍精品视频在线观看 | 色综合久久天天影视网 | 女性特黄一级毛片 | 成人激情在线视频 | 国产精品每日更新 | 久草美女视频 | 欧美毛片性视频区 | 在线97| 黄片毛片免费在线观看 | 久久久久依人综合影院 | 国产精品精品 | 五月婷婷激情综合 | 亚洲日韩欧美一区二区在线 | 一区二区三区免费在线 | 久久这里只有精品免费看青草 | 天天插夜夜爽 | 国产综合成色在线视频 |