好的,您已经迈出了很好的第一步,认识到Web.config只是另一个依赖项,并将其包装到ConfigProvider中进行注入是一个很好的解决方案。
但是,您开始陷入MVC的设计问题之一-即,为了实现DI友好性,属性应仅提供元数据,而从不实际定义行为。这不是测试方法的问题,而是滤波器设计方法的问题。
正如文章中指出的那样,您可以通过将操作过滤器属性分为两部分来解决此问题。
- 该属性不包含任何用于标记控制器和操作方法的行为。
- 一个DI友好类,该类实现IActionFilter并包含所需的行为。
该方法是使用IActionFilter来测试属性的存在,然后执行所需的行为。可以为动作过滤器提供所有依赖项,然后在组成应用程序时将其注入。
IConfigProvider provider = new WebConfigProvider();IActionFilter filter = new MaxLengthActionFilter(provider);GlobalFilters.Filters.Add(filter);
注意:
如果您需要过滤器的任何依赖项以使生命周期短于单例,则需要GlobalFilterProvider在此答案中使用as 。
MaxLengthActionFilter的实现如下所示:
public class MaxLengthActionFilter : IActionFilter{ public readonly IConfigProvider configProvider; public MaxLengthActionFilter(IConfigProvider configProvider) { if (configProvider == null) throw new ArgumentNullException("configProvider"); this.configProvider = configProvider; } public void onActionExecuted(ActionExecutedContext filterContext) { var attribute = this.GetMaxLengthAttribute(filterContext.ActionDescriptor); if (attribute != null) { var maxLength = attribute.MaxLength; // Execute your behavior here, and use the configProvider as needed } } public void onActionExecuting(ActionExecutingContext filterContext) { var attribute = this.GetMaxLengthAttribute(filterContext.ActionDescriptor); if (attribute != null) { var maxLength = attribute.MaxLength; // Execute your behavior here, and use the configProvider as needed } } public MaxLengthAttribute GetMaxLengthAttribute(ActionDescriptor actionDescriptor) { MaxLengthAttribute result = null; // Check if the attribute exists on the controller result = (MaxLengthAttribute)actionDescriptor .ControllerDescriptor .GetCustomAttributes(typeof(MaxLengthAttribute), false) .SingleOrDefault(); if (result != null) { return result; } // NOTE: You might need some additional logic to determine // which attribute applies (or both apply) // Check if the attribute exists on the action method result = (MaxLengthAttribute)actionDescriptor .GetCustomAttributes(typeof(MaxLengthAttribute), false) .SingleOrDefault(); return result; }}并且, 不应包含任何行为的 属性 应 如下所示:
// This attribute should contain no behavior. No behavior, nothing needs to be injected.[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]public class MaxLengthAttribute : Attribute{ public MaxLengthAttribute(int maxLength) { this.MaxLength = maxLength; } public int MaxLength { get; private set; }}使用更宽松的耦合设计,测试属性的存在要简单得多。
[TestMethod]public void base_controller_must_have_MaxLengthFilter_attribute(){ var att = typeof(baseController).GetCustomAttribute<MaxLengthAttribute>(); Assert.IsNotNull(att);}


