【发布时间】:2011-08-22 20:58:19
【问题描述】:
我正在尝试注册一个可以解析如下定义的事件处理程序数组的工厂:
public interface IEvent { }
public class EventA : IEvent { }
public class EventB : IEvent { }
public class EventC : IEvent { }
public interface IHandler<TEvent> where TEvent : IEvent
{
void Handle(TEvent ev);
}
public class HandlerX : IHandler<EventA>, IHandler<EventB>
{
public void Handle(EventA ev)
{
throw new NotImplementedException("handle EventA");
}
public void Handle(EventB ev)
{
throw new NotImplementedException("handle EventB");
}
}
public class HandlerY : IHandler<EventB>, IHandler<EventC>
{
public void Handle(EventB ev)
{
throw new NotImplementedException("handle EventB");
}
public void Handle(EventC ev)
{
throw new NotImplementedException("handle EventC");
}
}
public interface HandlerFactory
{
object[] GetHandlersForEvent(IEvent ev);
}
基本上对于每个事件,我可以有更多的处理程序,每个处理程序可以处理多个事件。我还希望工厂返回 object[] 因为在运行时我不知道会返回什么封闭的泛型类型。
我尝试了 Krzysztof Koźmic http://kozmic.pl/2010/03/11/advanced-castle-windsor-ndash-generic-typed-factories-auto-release-and-more/ 描述的方法 但仍然有问题。 基本上我的问题归结为从 DefaultTypedFactoryComponentSelector 派生的自定义类型返回什么类型。
我尝试了以下多种变体:
public class HandlerSelector : DefaultTypedFactoryComponentSelector
{
protected override TypedFactoryComponent BuildFactoryComponent(MethodInfo method, string componentName, Type componentType, System.Collections.IDictionary additionalArguments)
{
Type eventType = null;
foreach (var k in additionalArguments.Values)
{
eventType = k.GetType();
}
var handlerType = typeof(IHandler<>).MakeGenericType(eventType);
var handlerArrayType = handlerType.MakeArrayType();
//return handlerArrayType;
return new TypedFactoryComponentCollection(handlerType, additionalArguments);
}
protected override Type GetComponentType(MethodInfo method, object[] arguments)
{
return typeof (object);
/*
var message = arguments[0];
var handlerType = typeof(IHandler<>).MakeGenericType(message.GetType());
var handlerArrayType = handlerType.MakeArrayType();
return handlerArrayType;
*/
}
/*
public TypedFactoryComponent SelectComponent(MethodInfo method, Type type, object[] arguments)
{
var message = arguments[0];
var handlerType = typeof(IHandler<>).MakeGenericType(message.GetType());
var result = new TypedFactoryComponentCollection(handlerType.MakeArrayType(), new Arguments(arguments));
return result;
}*/
}
Windsor 安装程序定义为:
public class Installer : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>()
.Register(
Component.For<HandlerSelector>().ImplementedBy<HandlerSelector>(),
Component.For<AutoReleaseHandlerInterceptor>(),
AllTypes.FromAssemblyContaining<Program>()
.BasedOn(typeof(IHandler<>))
.WithService.Base()
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient)
.Interceptors<AutoReleaseHandlerInterceptor>()),
Component.For<HandlerFactory>().AsFactory(c => c.SelectedWith<HandlerSelector>()));
}
}
调用 factory.GetHandlersForEvent(ev) 时;我收到一个抱怨数组类型不匹配的异常: “试图以与数组不兼容的类型访问元素。”
堆栈跟踪:
在 System.Collections.Generic.Dictionary2.ValueCollection.CopyTo(TValue[] array, Int32 index)2.ValueCollection.System.Collections.ICollection.CopyTo(数组数组,Int32 索引)
at System.Collections.Generic.Dictionary
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs:line 285 中的 Castle.MicroKernel.DefaultKernel.ResolveAll(类型服务,IDictionary 参数)
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\TypedFactoryComponentCollection.cs:line 39 中的 Castle.Facilities.TypedFactory.TypedFactoryComponentCollection.Resolve(IKernel kernel)
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs:line 173 中的 Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation 调用)
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs:line 83 中的 Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation 调用)
在 Castle.DynamicProxy.AbstractInvocation.Proceed()
在 Castle.Proxies.HandlerFactoryProxy.GetHandlersForEvent(IEvent ev)
在 c:\users\user\documents\visual studio 2010\Projects 中的 CastleWindsorTests.Program.TryIt(HandlerFactory factory)
如何实现 HandlerSelector 以便它与定义为返回 object[] 的工厂一起工作,而运行时的真实对象是封闭的泛型类型? 我会很高兴看到一些现有的文档,其中包含 ITypedFactoryComponentSelector / DefaultTypedFactoryComponentSelector 实现者的指南。是的,我尝试了http://docs.castleproject.org/(S(kwaa14uzdj55gv55dzgf0vui))/Windsor.Typed-Factory-Facility-interface-based-factories.ashx,但这里没有太多关于上述类型的内容。
我真的不想引入服务定位器(而不是工厂);)。
【问题讨论】:
标签: dependency-injection castle-windsor