在阅读完这里的问题后;并尝试遵循http://docs.castleproject.org/(X(1)S(0w0clnf021ee0445lol02255))/Default.aspx?Page=Typed-Factory-Facility-interface-based-factories&NS=Windsor&AspxAutoDetectCookieSupport=1中提供的说明,我必须在某处缺少链接。
总结一下我正在尝试做的事情:
我有一个工厂接口:
public interface IFacilityTypeDescriptionBuilderFactory
{
IFacilityTypeDescriptionBuilder CreateFacilityTypeDescriptionBuilder(Facility facility);
}
这个工厂的目的是根据提供的设施的特定业务规则和属性值,将返回其他类型。显然它将是IFacilityTypeDescriptionBuilder类型,但是具体类型将由工厂确定(但不由其实例化)。
为此,最直接的实现是注册IFacilityTypeDescriptionBuilderFactory服务,执行.ImplementedBy,然后仅让工厂实例化返回的对象。但是这样做时,我将不得不使用/引用/对服务定位器具有依赖性,所以我宁愿不这样做。
根据上面的帖子;通过使用ITypedFactoryComponentSelector,我应该能够实现我想做的事情-并向Windsor指示如何做。不需要IFacilityTypeDescriptionBuilderFactory的具体实现。
这是我目前正在做的事情:
args.Container.AddFacility<TypedFactoryFacility>();
args.Container.Register(
Component.For<IFacilityTypeDescriptionBuilderFactory>()
.AsFactory()
);
args.Container.Register(
Component.For<ITypedFactoryComponentSelector>()
.ImplementedBy<FacilityTypeDescriptionBuilderComponentSelector>()
);
然后是我的ComponentSelector:
public class FacilityTypeDescriptionBuilderComponentSelector : DefaultTypedFactoryComponentSelector
{
protected override System.Type GetComponentType(System.Reflection.MethodInfo method, object[] arguments)
{
if (method.Name == "CreateFacilityTypeDescriptionBuilder" && arguments.Length == 1 && arguments[0] is Facility)
{
var facility = arguments[0] as Facility;
switch (facility.FacilityTypeId)
{
case 1:
return typeof (RestaurantFacilityTypeDescriptionBuilder);
default:
return typeof (EmptyFacilityTypeDescriptionBuilder);
}
}
return base.GetComponentType(method, arguments);
}
}
有了所有这些之后,我现在尝试断言所有内容均已正确连接。事实并非如此;在此示例中,“ b”最终为NULL。
var f = new Facility();
f.FacilityTypeId = 1;
var b = args.Container.Resolve<IFacilityTypeDescriptionBuilderFactory>().CreateFacilityTypeDescriptionBuilder(f);
Assert.IsNotNull(b as RestaurantFacilityTypeDescriptionBuilder, "b");
根据我的判断,从未调用ComponentSelector-或至少没有以我期望的方式调用它。我得到的错误是这样的:
找不到支持服务XYZ.Common.Builders.IFacilityTypeDescriptionBuilder的组件
因此,基本上,温莎意识到必须从.CreateFacilityTypeDescriptionBuilder(f)方法调用中返回IFacilityTypeDescriptionBuilder-只是不知道如何。
谁能指出我在这里所缺少的东西吗?
最佳答案
你需要
.AsFactory(f => f.SelectedWith<FacilityTypeDescriptionBuilderComponentSelector>())