给定一组具有以下依赖结构的对象(使用构造函数注入):
IWidget 依赖于 IWidgetDependency 依赖于 IDependencyConfiguration
是否可以设置IDependencyConfiguration实现的生命周期,以便它们绑定到特定的IWidget实现,即使我想在多个位置使用相同的IWidgetDependency实现?
这是代码;它将正确解析FooWidget,但无法解析BarWidget,我不明白为什么:
var container = new WindsorContainer();
container.Register(Component.For<FooWidget>(), Component.For<BarWidget>());
container.Register(Component.For<IWidgetDependency>().ImplementedBy<WidgetDependency>().LifestyleTransient());
container.Register(
Component.For<IDependencyConfiguration>().ImplementedBy<FooConfiguration>().LifestyleBoundTo<FooWidget>(),
Component.For<IDependencyConfiguration>().ImplementedBy<BarConfiguration>().LifestyleBoundTo<BarWidget>());
// I expect to resolve a FooWidget that contains a WidgetDependency that contains a FooConfiguration
var fooWidget = container.Resolve<FooWidget>(); // Works fine
// I expect to resolve a BarWidget that contains a WidgetDependency that contains a BarConfiguration
var barWidget = container.Resolve<BarWidget>(); // Scope was not available for 'FooConfiguration'