使用Castle Windsor子容器来解析具有特定实例的类型 [英] Using Castle Windsor child containers to resolve a type with a specific instance
问题描述
我目前正在使用Castle Windsor的子容器功能在工厂方法中使用特定实例覆盖特定类型的注册。我纯粹是在使用子容器,因此对于单个解决方案来说注册是临时的-换句话说,我不希望该注册影响该类型的所有决议。
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.
我有一个Func,它充当工厂 Func< IReportCategory,IReportCategoryViewModel>
-我为它提供了一个IReportCategory的实现,它返回了一个IReportCategoryViewModel的新实例。 (IReportCategoryViewModel依赖于IReportCategory。)
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)));
ResolveCategoryViewModelFactory
的实现方式如下:
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>();
}
}
以上方法实现的是IReportCategoryViewModel的分辨率,注入IReportCategory的特定实例作为依赖项。如果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.
我随后可以按如下方式使用Func:
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?
感谢您的帮助。
推荐答案
绝对有更好的方法,而您正在使用的代码现在存在错误-它将尝试释放所有组件实例处置子容器时您正在解决,因此在您有机会使用子容器之前,它们可能不可用(处置)。
Absolutely there are better ways to go, and the code you're using right now has a bug - it will try to release all the component instances you're resolving when you dispose of the child container, therefore they might be unusable (disposed) before you even get a chance to use them.
如果我理解您的解释正确地感觉就像是类型化工厂的工作。
If I understand your explanation correctly it feels like a job for typed factories.
这篇关于使用Castle Windsor子容器来解析具有特定实例的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!