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);
I found a solution, but I’m hoping that someone can suggest a better one. Please give me some critic on the selected solution:
I did two things:
Created a custom lifestyle manager which added the ability to say that components would act as transient outside of a lifestyle scope and as a singleton within one.
Created a lifestyle scope which I can use to specify a “context-boundary”.
There is one thing I don’t like about this solution and that is the fact that you have to know about the context-issue from the call-site. I could abstract this by moving the lifestyle scope into the viewmanager, but then the viewmanager needs to have an ioc dependency which will require some extra stubbing when testing the viewmanager.