Consider the following classes:
public interface IView
{
/// <summary>
/// Ignore this (its only for the assertion)
/// </summary>
ITask Task { get; }
}
public class ViewA : IView
{
private readonly ITask _task;
public ViewA(ITask task)
{
_task = task;
}
public ITask Task
{
get { return _task; }
}
}
public class ViewB : IView
{
private readonly ITask _task;
public ViewB(ITask task)
{
_task = task;
}
public ITask Task
{
get { return _task; }
}
}
public class ViewManager
{
private readonly List<IView> _views;
public ViewManager(IView[] views)
{
_views = new List<IView>(views);
}
public ReadOnlyCollection<IView> Views
{
get { return new ReadOnlyCollection<IView>(_views); }
}
}
And finally what I am trying to accomplish:
[Fact]
public void Configure_TwoServicesWithDependencyToTransientComponent_BothShareComponent()
{
// arrange
var windsorContainer = new WindsorContainer();
windsorContainer.Kernel.Resolver.AddSubResolver(new ArrayResolver(windsorContainer.Kernel));
windsorContainer.Register(Component.For<ITask>()
.ImplementedBy<TaskWithNoDependencies>()
.Named("task")
.LifeStyle.Transient);
windsorContainer.Register(Component.For<IView>().ImplementedBy<ViewA>().LifeStyle.Transient);
windsorContainer.Register(Component.For<IView>().ImplementedBy<ViewB>().LifeStyle.Transient);
windsorContainer.Register(Component.For<ViewManager>()
.LifeStyle.Transient);
// act
ViewManager service = windsorContainer.Resolve<ViewManager>();
// assert
Assert.Same(service.Views[0].Task, service.Views[1].Task);
}
How can I make Windsor create an instance of ITask so thats its shared by ViewA and ViewB when resolving ViewManager? I tried the following but it did not work:
windsorContainer.Register(Component.For<ViewManager>()
.DynamicParameters((k, d) =>
{
d["task"] = k.Resolve<ITask>();
})
.LifeStyle.Transient);