使用Fluent界面在Castle Windsor中注册所有课程
我有一个抽象的基类Search
。 一个抽象类IndexedRepositorySearch
来自Search
。 一个抽象类FacetSearch
来自IndexedRepositorySearch
。 具体类IngredientFacetSearch
和RecipeFacetSearch
来自FacetSearch
。
目前,我们正在注册来自Search
with Castle Windsor的所有内容,如下所示:
AllTypes.FromAssembly(assembly) .BasedOn() .WithServiceBase() .WithServiceSelf() .LifestyleTransient() .AllowMultipleMatches()
当我们打电话的时候
_container.ResolveAll(new { searchInput = input, searchResults });
它无法解决容器中的任何问题。 当我在注册所有内容后在容器上放置一个断点并检查调试器中的AllComponents
时,我看到了IngredientFacetSearch
和RecipeFacetSearch
,但它们每个只有两个与之关联的服务:他们的自我和Search
,他们的基础。 鉴于他们使用.WithServiceBase()
和.WithServiceSelf()
注册,您可以期待。
所以问题是如何通过调用ResolveAll()
来解决它们?
我确信这很简单,但如果我能找到它,我会很高兴。
提前致谢。
我们有一个类似的问题,但只需要注册第一个抽象的基础。 我使用WithServiceSelect
解决了它,但将其包装成一个扩展方法,以便使用:
AllTypes.FromAssembly(assembly) .BasedOn() .WithServiceLastAbstractBase(),
扩展方法的定义是:
public static BasedOnDescriptor WithServiceLastAbstractBase(this BasedOnDescriptor basedOn) { return basedOn.WithServiceSelect(selectLastAbstractBase); } private static IEnumerable 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 }; }