咱鳥(niǎo)語(yǔ)很差,文采為0,重在概述,請(qǐng)多包涵...
使用新版的Ninject MVC3時(shí)發(fā)現(xiàn)過(guò)濾器的DI跟原來(lái)(kernel.BindFilter)不一樣了,某度上沒(méi)找到中文的,遂摸到Ninject老家找到相關(guān)文檔,看了半天,干脆在這里留個(gè)底,方便自己將來(lái)回味,也給后來(lái)人指?jìng)€(gè)路...寫(xiě)到一半突發(fā)奇想,既然文言文咱寫(xiě)不好,干脆寫(xiě)個(gè)口語(yǔ)賣(mài)萌的吧.....想看渣文言文的同學(xué)請(qǐng)反白...
環(huán)境:MVC3 + Ninject 3.0.1.10 + Ninject.MVC3 3.0.0.6?
Dependency injection for filters
依賴(lài)注入到MVC過(guò)濾器
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使用了全新的模式來(lái)配置控制器和動(dòng)作上的過(guò)濾器。雖然過(guò)濾器特性注入依然被支持,但是我們建議使用新模式來(lái)配置過(guò)濾器。因?yàn)樗哂兄С謽?gòu)造函數(shù)注入的優(yōu)勢(shì),不再需要注入特性了。
新的N-MVC3用了新配置模式啦,原來(lái)的不管用鳥(niǎo),新配置模式支持構(gòu)造函數(shù)注入,無(wú)需注入特性鳥(niǎo)...
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.
首先,你需要建立一個(gè)實(shí)現(xiàn)了過(guò)濾器接口(如IActionFilter)的過(guò)濾器類(lèi),將依賴(lài)項(xiàng)添加到構(gòu)造函數(shù)中。下面的例子實(shí)現(xiàn)了一個(gè)日志過(guò)濾器,其中包含一個(gè)Log依賴(lài)項(xiàng),以及用于記錄日志的級(jí)別。
建一個(gè)實(shí)現(xiàn)了過(guò)濾器接口的過(guò)濾器類(lèi),給你想注入的東東扔到構(gòu)造函數(shù)里面,這個(gè)例子中有日志及級(jí)別依賴(lài)。
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#用戶(hù)應(yīng)該都能看懂這個(gè)過(guò)濾器想干嘛...
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.
要應(yīng)用這個(gè)過(guò)濾器到控制器或動(dòng)作,我們需要指定一個(gè)綁定。但是不像其他的綁定,舊的BindFilter 綁定方法被換為Bind方法。另外,你必須指定過(guò)濾器的作用域及過(guò)濾順序。關(guān)于這兩個(gè)參數(shù)的更多信息,請(qǐng)參見(jiàn)Brad Wilsons的博客。
下面的例子中,日志過(guò)濾器被應(yīng)用到控制器上的每個(gè)動(dòng)作中并配置日志級(jí)別信息。(構(gòu)造器注入)
想給這個(gè)過(guò)濾器弄控制器上面去,舊的BindFiler方法不管用了,需要模塊綁定啦。綁定的時(shí)候你得指定著過(guò)濾器的作用域跟執(zhí)行順序,想看這哥倆更多信息的,請(qǐng)猛擊布拉德威爾遜老濕的博客文(見(jiàn)上)。
下面代碼中,日志過(guò)濾器被注冊(cè)到每一個(gè)控制器上,同時(shí)配置了日志級(jí)別信息。(構(gòu)造器注入)
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:
更多信息請(qǐng)參見(jiàn):
過(guò)濾器條件綁定
過(guò)濾器配置
更多相關(guān)信息請(qǐng)猛擊:過(guò)濾器條件綁定,過(guò)濾器配置(見(jiàn)上)
完
原文:
Dependency injection for filters
補(bǔ):找不到BindFilter的同學(xué),請(qǐng)using Ninject.Web.Mvc.FilterBindingSyntax;
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
