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