I'm currently using Castle Windsor's child container functionality to override the registration of a particular type with a specific instance in a factory method. I am using the child containers purely so that the registration is temporary for a single resolution - in other words, I don't want the registration to affect all resolutions for that type.
Perhaps some code will explain what I mean.
I have a Func which acts as a factory Func<IReportCategory, IReportCategoryViewModel>
- I give it an implementation of an IReportCategory and it returns a new instance of an IReportCategoryViewModel. (IReportCategoryViewModel has a dependency on IReportCategory).
This is registered with Castle Windsor as follows:
container.Register(
Component.For<Func<IReportCategory, IReportCategoryViewModel>>().Instance(
category => ResolveCategoryViewModelFactory(container, category)));
Where ResolveCategoryViewModelFactory
is implemented as follows:
private CategoryViewModel ResolveCategoryViewModelFactory(IWindsorContainer container, IReportCategory category)
{
using (IWindsorContainer childContainer = new WindsorContainer())
{
childContainer.Register(Component.For<IReportCategory>().Instance(category));
container.AddChildContainer(childContainer);
return childContainer.Resolve<IReportCategoryViewModel>();
}
}
What the above method achieves is the resolution of IReportCategoryViewModel, injecting the specific instance of IReportCategory as a dependency. If IReportCategoryViewModel has other dependencies that need satisfying, then these get automatically injected by the container.
I can subsequently use the Func as follows:
public class Test
{
private readonly Func<IReportCategory, IReportCategoryViewModel> factory;
public Test(Func<IReportCategory, IReportCategoryViewModel> factory)
{
this.factory = factory;
}
public void ResolveTest()
{
// Create a category (note: this would probably be resolved from the container in some way)
IReportCategory category = new ReportCategory();
// Call into the factory to resolve the view model
IReportCategoryViewModel vm = factory(category);
}
...
Question: Does this seem like a suitable thing to do? From the impression I get, child containers are not recommended in Castle Windsor - is there another way of achieving the same result?
Thanks for your help.