栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用构造函数参数测试动作过滤器的存在?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用构造函数参数测试动作过滤器的存在?

好的,您已经迈出了很好的第一步,认识到Web.config只是另一个依赖项,并将其包装到ConfigProvider中进行注入是一个很好的解决方案。

但是,您开始陷入MVC的设计问题之一-即,为了实现DI友好性,属性应仅提供元数据,而从不实际定义行为。这不是测试方法的问题,而是滤波器设计方法的问题。

正如文章中指出的那样,您可以通过将操作过滤器属性分为两部分来解决此问题。

  1. 该属性不包含任何用于标记控制器和操作方法的行为。
  2. 一个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);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/594671.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号