实现新的ISiteMapNodeVisibilityProvider

本文关键字:ISiteMapNodeVisibilityProvider 实现 | 更新日期: 2023-09-27 18:30:45

我有这样的实现ISiteMapNodeVisibilityProvider

public bool IsVisible(SiteMapNode node, HttpContext context, IDictionary<string, object> sourceMetadata)
        {
            return !context.Request.IsAuthenticated;
        }

但是在新版本中,要更改的内容ISiteMapNodeVisibilityProvider看起来像这样:

public interface ISiteMapNodeVisibilityProvider
    {
        bool AppliesTo(string providerName);
        bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata);
    }

我只是想知道如何在新版本中实现我的旧逻辑? 它不包含context

实现新的ISiteMapNodeVisibilityProvider

我认为

应该这样做:

public class AuthenticatedVisibilityProvider : SiteMapNodeVisibilityProviderBase
    {
        #region ISiteMapNodeVisibilityProvider Members
        /// <summary>
        /// Determines whether the node is visible.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="sourceMetadata">The source metadata.</param>
        /// <returns>
        ///     <c>true</c> if the specified node is visible; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata)
        {
            return HttpContext.Current.Request.IsAuthenticated;
        }
        #endregion
    }

这个怎么样:

public class MyImplementation:ISiteMapNodeVisibilityProvider
{
    HttpContext _context;
    public MyImplementation(HttpContext context)
    {
          _context = context;
    }
    public bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata){
       return !_context.Request.IsAuthenticated;
    }
    //example implementation of AppliesTo from
    //one of base classes of MVCSiteMapProvider
    //https://github.com/maartenba/MvcSiteMapProvider/blob/master/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNodeVisibilityProviderBase.cs
    public virtual bool AppliesTo(string providerName)
    {
        return this.GetType().ShortAssemblyQualifiedName().Equals(providerName, StringComparison.InvariantCulture);
    }
}

换句话说,使用上下文作为参数实例化类。

ISiteMapNodeVisibilityProvider provider = new MyImplementation(httpContext);
bool isVisible = provider.IsVisible;

正如升级文档中指出的,您不需要实现 AppliesTo()。您可以从 SiteMapNodeVisibilityProviderBase 继承:

// Using Internal DI
public class MyImplementation : SiteMapNodeVisibilityProviderBase
{
    HttpContextBase _context;
    public MyImplementation()
    {
          _context = new HttpContextWrapper(HttpContext.Current);
    }
    public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata){
       return !_context.Request.IsAuthenticated;
    }
}
// Using External DI
public class MyImplementation : SiteMapNodeVisibilityProviderBase
{
    HttpContextBase _context;
    public MyImplementation(HttpContextBase context)
    {
          _context = context;
    }
    public override bool IsVisible(ISiteMapNode node, IDictionary<string, object> sourceMetadata){
       return !_context.Request.IsAuthenticated;
    }
}

传递 HttpContextBase 也比传递 HttpContext 更好,因为如果您决定开始进行单元测试,它很容易被嘲笑。然后,可以使用名为 HttpContextWrapper 的 HttpContextBase 的 .NET 默认实现来包装静态实例。

请注意,内部 DI 容器需要默认的公共构造函数,因为它使用反射来实例化类。但是,如果使用外部 DI 容器,则可以将 HttpContextBase 传递到类中,因为如果已向容器注册构造函数参数,它将解析构造函数参数(同样,它将使其可进行单元测试)。

在结构映射(用于外部 DI)中,注册码如下所示:

container.For<System.Web.HttpContext>().Use(t => System.Web.HttpContext.Current);
container.For<System.Web.HttpContextBase>().Use<System.Web.HttpContextWrapper>();
相关文章:
  • 没有找到相关文章