0
我想綁定一個服務並在我們的應用程序中使用Ninject指定構造函數參數。構造函數參數是一個可以從查詢字符串或cookie中提取的值。我們目前的代碼是這樣的Ninject調用方法返回構造函數參數
kernel.Bind<SomeService>()
.ToSelf()
.InRequestScope()
.WithConstructorArgument("someID", ctx =>
// Try to get it from the posted form values
System.Web.HttpContext.Current.Request.Form["someID"] != null ?
long.Parse(System.Web.HttpContext.Current.Request.Form["someID"]) :
// Try to get it from the query string
System.Web.HttpContext.Current.Request.QueryString["someID"] != null ?
long.Parse(System.Web.HttpContext.Current.Request.QueryString["someID"])
: 0);
這個工程,但很醜陋。我意識到還有其他一些方法可以完成此操作,例如傳遞Form值或QueryString值作爲參數,但我們希望在Binding中定義它。我們會非常願意做的是這樣的:
kernel.Bind<SomeService>()
.ToSelf()
.InRequestScope()
.WithConstructorArgument("someID", ctx => GetSomeID());
從我所知,這是不可能的。是否有另一種方法將構造函數參數注入邏輯分解爲另一種方法,因此我們不必嵌套一行if語句?
不應該'ParametersFromHttpContext'執行'IParameters'? – Sung