6

在我的 ASP.NET MVC 3 应用程序上,我定义了一个路由约束,如下所示:

public class CountryRouteConstraint : IRouteConstraint {

    private readonly ICountryRepository<Country> _countryRepo;

    public CountryRouteConstraint(ICountryRepository<Country> countryRepo) {
        _countryRepo = countryRepo;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {

        //do the database look-up here

        //return the result according the value you got from DB
        return true;
    }
}

我在我的应用程序上使用 Ninject 作为 IoC 容器,它实现IDependencyResolver并注册了我的依赖项:

    private static void RegisterServices(IKernel kernel) {

        kernel.Bind<ICountryRepository<Country>>().
            To<CountryRepository>();
    }    

如何以依赖注入友好的方式使用此路由约束?

编辑

我找不到通过这种对单元测试的依赖的方法:

[Fact]
public void country_route_should_pass() {

    var mockContext = new Mock<HttpContextBase>();
    mockContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/countries/italy");

    var routes = new RouteCollection();
    TugberkUgurlu.ReservationHub.Web.Routes.RegisterRoutes(routes);

    RouteData routeData = routes.GetRouteData(mockContext.Object);

    Assert.NotNull(routeData);
    Assert.Equal("Countries", routeData.Values["controller"]);
    Assert.Equal("Index", routeData.Values["action"]);
    Assert.Equal("italy", routeData.Values["country"]);
}
4

3 回答 3

5

虽然@Darin 建议的方法有效,但注入的依赖项需要在应用程序的整个生命周期内保持活跃。例如,如果依赖项的范围在请求范围内,那么它将适用于第一个请求,而不适用于之后的每个请求。

您可以通过对路由约束使用非常简单的 DI 包装器来解决此问题。

public class InjectedRouteConstraint<T> : IRouteConstraint where T : IRouteConstraint
{
private IDependencyResolver _dependencyResolver { get; set; }
public InjectedRouteConstraint(IDependencyResolver dependencyResolver)
{
    _dependencyResolver = dependencyResolver;
}

public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
    return _dependencyResolver.GetService<T>().Match(httpContext, route, parameterName, values, routeDirection);
}
}

然后像这样创建您的路线

var _dependencyResolver = DependencyResolver.Current; //Get this from private variable that you can override when unit testing

routes.MapRoute(
  "Countries",
  "countries/{country}",
  new { 
      controller = "Countries", 
      action = "Index" 
  },
  new { 
      country = new InjectedRouteConstraint<CountryRouteConstraint>(_dependencyResolver);
  }
);

编辑:试图使其可测试。

于 2012-05-21T21:12:09.543 回答
4
routes.MapRoute(
    "Countries",
    "countries/{country}",
    new { 
        controller = "Countries", 
        action = "Index" 
    },
    new { 
        country = new CountryRouteConstraint(
            DependencyResolver.Current.GetService<ICountryRepository<Country>>()
        ) 
    }
);
于 2011-11-29T09:59:09.757 回答
0

您可以尝试使用属性注入和IDependencyResolver

public class CountryRouteConstraint : IRouteConstraint {
    [Inject]
    public ICountryRepository<Country> CountryRepo {get;set;}
}

并非所有的 IoC 容器都能很好地适应这一点;Ninject 有效。

我不确定这是否可行,不幸的是无法测试此自动取款机。

另一种选择是使用服务定位器,您可以在其中提供一个可用的静态对象,该对象负责检索接口的实现。

于 2011-11-29T09:54:21.143 回答