Ninject 绑定需要当前 Request.Url 的类型
Ninject binding for a type which requires the current Request.Url
我在基于 MVC5 的网站中使用 Ninject 3,并试图弄清楚如何让 DI 与测试传递到其构造函数的 Uri.Host
值的属性的类型一起工作.我希望绑定以某种方式提供当前 URL。我最初尝试的最小结构是:
public class StructuredUrlTester : IStructuredUrlTester
{
// Expose public getters for parts of the uri.Host value
bool MyBooleanProperty { get; private set; }
public StructuredUrlTester(Uri uri)
{
// Test the value of uri.Host and extract parts via regex
}
}
// In Global.asax.cs
public class MvcApplication : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
kernel.Bind<IStructuredUrlTester>()
.To<StructuredUrlTester>()
.InTransientScope();
.WithConstructorArgument("uri", Request.Url);
}
}
// In MyController.cs
public class MyController : Controller
{
private readonly IStructuredUrlTester _tester;
public ContentPageController(IStructuredUrlTester tester)
{
this._tester = tester;
}
public ActionResult Index()
{
string viewName = "DefaultView";
if (this._tester.MyBooleanProperty)
{
viewName = "CustomView";
}
return View(viewName);
}
}
由于 CreateKernel()
调用发生在 Request
对象可用之前,.WithConstructorArgument()
部分抛出异常 ("System.Web.HttpException: Request is not available in this context")。
我如何提供接口到具体类型的绑定,同时还能够提供例如HttpContext.Current.Request.Url
值(在 Controller 中可用)到具体类型的构造函数,在 运行 可用时?
在抽象中包装所需的功能:
public interface IUriProvider {
Uri Current { get; }
}
重构测试器 class:
public class StructuredUrlTester : IStructuredUrlTester {
// Expose public getters for parts of the uri.Host value
bool MyBooleanProperty { get; private set; }
public StructuredUrlTester(IUriProvider provider) {
Uri uri = provider.Current;
// Test the value of uri.Host and extract parts via regex
}
}
提供程序实现应包装 Request.Url
:
public class UriProvider : IUriProvider {
public Uri Current { get { return HttpContext.Current.Request.Url; } }
}
请注意 Current
属性 实际上应该在 HttpContext
及其请求可用的控制器的操作中调用。
我在基于 MVC5 的网站中使用 Ninject 3,并试图弄清楚如何让 DI 与测试传递到其构造函数的 Uri.Host
值的属性的类型一起工作.我希望绑定以某种方式提供当前 URL。我最初尝试的最小结构是:
public class StructuredUrlTester : IStructuredUrlTester
{
// Expose public getters for parts of the uri.Host value
bool MyBooleanProperty { get; private set; }
public StructuredUrlTester(Uri uri)
{
// Test the value of uri.Host and extract parts via regex
}
}
// In Global.asax.cs
public class MvcApplication : NinjectHttpApplication
{
protected override IKernel CreateKernel()
{
kernel.Bind<IStructuredUrlTester>()
.To<StructuredUrlTester>()
.InTransientScope();
.WithConstructorArgument("uri", Request.Url);
}
}
// In MyController.cs
public class MyController : Controller
{
private readonly IStructuredUrlTester _tester;
public ContentPageController(IStructuredUrlTester tester)
{
this._tester = tester;
}
public ActionResult Index()
{
string viewName = "DefaultView";
if (this._tester.MyBooleanProperty)
{
viewName = "CustomView";
}
return View(viewName);
}
}
由于 CreateKernel()
调用发生在 Request
对象可用之前,.WithConstructorArgument()
部分抛出异常 ("System.Web.HttpException: Request is not available in this context")。
我如何提供接口到具体类型的绑定,同时还能够提供例如HttpContext.Current.Request.Url
值(在 Controller 中可用)到具体类型的构造函数,在 运行 可用时?
在抽象中包装所需的功能:
public interface IUriProvider {
Uri Current { get; }
}
重构测试器 class:
public class StructuredUrlTester : IStructuredUrlTester {
// Expose public getters for parts of the uri.Host value
bool MyBooleanProperty { get; private set; }
public StructuredUrlTester(IUriProvider provider) {
Uri uri = provider.Current;
// Test the value of uri.Host and extract parts via regex
}
}
提供程序实现应包装 Request.Url
:
public class UriProvider : IUriProvider {
public Uri Current { get { return HttpContext.Current.Request.Url; } }
}
请注意 Current
属性 实际上应该在 HttpContext
及其请求可用的控制器的操作中调用。