我想注册实现字典适配器的多个组件,但AllTypes.From ..只选择类类型。我想做这样的事情:
container.Register(AllTypes.FromAssemblyContaining<IFooSettings>()
.Where(type => type.IsInterface)
.Configure(component =>
component.UsingFactoryMethod((kernel, model, creationContext) => new DictionaryAdapterFactory().GetAdapter(creationContext.RequestedType, ConfigurationManager.AppSettings))));
现在我似乎无法创建自己的“AllTypes”版本,因为FromTypesDescriptor ctor是内部的。我有什么想法可以做到这一点吗?
答案 0 :(得分:0)
我这样做
container.Register(
AllTypes
.FromThisAssembly()
.Pick()
.WithService.DefaultInterface()
.Configure(r => r.LifeStyle.Transient));
这会根据匹配的接口和类名注册组件。
因此,如果我向Castle询问名为IFooSettings
的界面,那么Castle将默认返回类FooSettings
。
您可以使用一系列规则进行注册,您可以看到它们here。建议不要使用您在示例中使用的Factory方法。
答案 1 :(得分:0)
现在可以在Castle 3.3.0中使用:
var dictionaryAdapterFactory = new DictionaryAdapterFactory();
container.Register(
Types.FromThisAssembly().Where(t => t.Name.EndsWith("Settings") && t.IsInterface)
.Configure(component => component.UsingFactoryMethod((kernel, model, creationContext) => dictionaryAdapterFactory.GetAdapter(creationContext.RequestedType, ConfigurationManager.AppSettings))));
你必须使用“类型”,它也会选择接口