I am trying to register a factory that can resolve an array of event handlers defined as follows:
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);
}
Basically, for each event, I can have more handlers, and each handler can handle several events. I also want the factory to return the object [], because at runtime I do not know which private types will be returned.
I tried the approach described by Krzysztof Koźmic http://kozmic.pl/2010/03/11/advanced-castle-windsor-ndash-generic-typed-factories-auto-release-and-more/
but still have problems. Basically my question comes down to what types are returned from my custom type derived from DefaultTypedFactoryComponentSelector.
I tried many options:
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 new TypedFactoryComponentCollection(handlerType, additionalArguments);
}
protected override Type GetComponentType(MethodInfo method, object[] arguments)
{
return typeof (object);
}
}
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.Dictionary 2.ValueCollection.CopyTo(TValue[] array, Int32 index)
at System.Collections.Generic.Dictionary 2.ValueCollection.System.Collections.ICollection.CopyTo( , Int32)
Castle.MicroKernel.DefaultKernel.ResolveAll( , IDictionary) e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs: 285
Castle.Facilities.TypedFactory.TypedFactoryComponentCollection.Resolve( IKernel) e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\TypedFactoryComponentCollection.cs: 39
Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve( IInvocation) e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs: 173
Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept( IInvocation) e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs: 83
Castle.DynamicProxy.AbstractInvocation.Proceed()
Castle.Proxies.HandlerFactoryProxy.GetHandlersForEvent(IEvent ev)
CastleWindsorTests.Program.TryIt(HandlerFactory factory) c:\users\user\documents\visual studio 2010\Projects
HandlerSelector , factory, [], ?
- ITypedFactoryComponentSelector/DefaultTypedFactoryComponentSelector. , http://docs.castleproject.org/(S(kwaa14uzdj55gv55dzgf0vui))/Windsor.Typed-Factory-Facility-interface-based-factories.ashx, .
( factory);).