如何一次使用多个全局ISiteMapNodeVisibilityProvider实现?

时间:2019-02-26 22:15:30

标签: asp.net-mvc castle-windsor structuremap mvcsitemapprovider

我正在尝试将以下StructureMap代码从MvcSiteMapProvider官方文档移植到温莎城堡。

// Visibility Providers

// Explicitly set the visibility providers, using CompositeSiteMapNodeVisibilityProvider to combine the AclModuleVisibilityProvider
// with all other ISiteMapNodeVisibilityProvider implementations.
this.For<ISiteMapNodeVisibilityProviderStrategy>().Use<SiteMapNodeVisibilityProviderStrategy>()
    .EnumerableOf<ISiteMapNodeVisibilityProvider>().Contains(x =>
        {
            x.Type<CompositeSiteMapNodeVisibilityProvider>()
                .Ctor<string>("instanceName").Is("filteredAndTrimmedAndCustom")
                .EnumerableOf<ISiteMapNodeVisibilityProvider>().Contains(y =>
                    {
                        // Note that the visibility providers are executed in
                        // the order specified here, but execution stops when
                        // the first visibility provider returns false.
                        y.Type<FilteredSiteMapNodeVisibilityProvider>();
                        y.Type<TrimEmptyGroupingNodesVisibilityProvider>();
                        y.Type<CustomVisibilityProvider>();
                    });
        })
    .Ctor<string>("defaultProviderName").Is("filteredAndTrimmedAndCustom");

这是我的温莎城堡代码:

// Visibility Providers
container.Register(
  Component.For<ISiteMapNodeVisibilityProvider>()
    .Named("filteredAndTrimmedAndCustom")
    .ImplementedBy<CompositeSiteMapNodeVisibilityProvider>()
    .DependsOn(Dependency.OnValue(
        "instanceName",
        "filteredAndTrimmedAndCustom"))
    .DependsOn(Dependency.OnComponentCollection(
        typeof(FilteredSiteMapNodeVisibilityProvider),
        typeof(TrimEmptyGroupingNodesVisibilityProvider),
        typeof(CustomVisibilityProvider))));
container.Register(
  Component.For<ISiteMapNodeVisibilityProviderStrategy>()
    .ImplementedBy<SiteMapNodeVisibilityProviderStrategy>()
    .DependsOn(Dependency.OnValue(
        "defaultProviderName",
        "filteredAndTrimmedAndCustom")));

问题在于,在这段代码之后,我在ISiteMapNodeVisibilityProvider的所有实现都被全局使用。如果我有一个名为``ABCVisibilityProvider`''的本地可见性提供程序,它将在全球范围内使用。这行似乎不应该按以下方式工作:

.DependsOn(Dependency.OnComponentCollection(
    typeof(FilteredSiteMapNodeVisibilityProvider),
    typeof(TrimEmptyGroupingNodesVisibilityProvider),
    typeof(CustomVisibilityProvider))));

我没有正确移植代码吗?

1 个答案:

答案 0 :(得分:1)

我没有为OnComponentCollection使用正确的方法重载。 CompositeSiteMapNodeVisibilityProvider的构造函数有一个名为siteMapNodeVisibilityProviders的参数。

.DependsOn(Dependency.OnComponentCollection(
    "siteMapNodeVisibilityProviders",
    typeof(FilteredSiteMapNodeVisibilityProvider),
    typeof(TrimEmptyGroupingNodesVisibilityProvider), 
    typeof(PropertyTypeGlobalNodeVisibilityProvider)))