gpt4 book ai didi

c# - 使用 Fluent 接口(interface)在 CaSTLe Windsor 中从上到下注册所有类

转载 作者:行者123 更新时间:2023-11-30 22:32:03 25 4
gpt4 key购买 nike

我有一个抽象基类 Search .一个抽象类 IndexedRepositorySearch源自 Search .一个抽象类 FacetSearch源自 IndexedRepositorySearch .具体类IngredientFacetSearchRecipeFacetSearch源自 FacetSearch .

目前我们正在注册从 Search 派生的所有内容与温莎城堡如下:

AllTypes.FromAssembly(assembly)
.BasedOn<Search.Search>()
.WithServiceBase()
.WithServiceSelf()
.LifestyleTransient()
.AllowMultipleMatches()

当我们调用

_container.ResolveAll<FacetSearch>(new { searchInput = input, searchResults });

它不解析容器中的任何内容。当我在所有内容都已注册并检查 AllComponents 后在容器上放置断点时在调试器中,我看到了 IngredientFacetSearchRecipeFacetSearch , 但他们每个人只有两个与之关联的服务:他们自己和 Search ,他们的基地。考虑到他们在 .WithServiceBase() 注册,您会期望的和 .WithServiceSelf() .

所以问题是如何通过调用 ResolveAll<FacetSearch>() 来解决它们?

我确定它很简单,但如果我能找到它,我会很高兴。

提前致谢。

最佳答案

我们遇到了类似的问题,但只需要注册第一个抽象的碱基。我使用 WithServiceSelect 解决了它,但将它包装成一个扩展方法以便使用:

AllTypes.FromAssembly(assembly)
.BasedOn<Search.Search>()
.WithServiceLastAbstractBase(),

扩展方法的定义是:

public static BasedOnDescriptor WithServiceLastAbstractBase(this BasedOnDescriptor basedOn)
{
return basedOn.WithServiceSelect(selectLastAbstractBase);
}

private static IEnumerable<Type> selectLastAbstractBase(Type type, Type[] basetypes)
{
var baseType = type;

do
{
baseType = baseType.BaseType;
} while (baseType != null && !baseType.IsAbstract);

if (baseType == null)
{
throw new ArgumentException("There are no abstract base types for: " + type);
}

return new [] { baseType };
}

关于c# - 使用 Fluent 接口(interface)在 CaSTLe Windsor 中从上到下注册所有类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com