开发者

Castle Windsor Typed Factory Facility with generics

I'm trying to register a factory that could resolve an array of event handlers defined as follow:

    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<E开发者_如何学PythonventB>, 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 multiple events. I also want the factory to return object[] because at runtime I don't know what closed generic types would be returned.

I tried the approach descirbed 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 boils down to what types to return from my custom type deriving from DefaultTypedFactoryComponentSelector.

I tried many variations of the following:

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;
        }*/
    }

with Windsor installer defined as:

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>()));
        }
    }

When calling factory.GetHandlersForEvent(ev); I get an exception complaining about array type mismatch: "Attempted to access an element as a type incompatible with the array."

Stack trace:

at System.Collections.Generic.Dictionary2.ValueCollection.CopyTo(TValue[] array, Int32 index)

at System.Collections.Generic.Dictionary2.ValueCollection.System.Collections.ICollection.CopyTo(Array array, Int32 index)

at Castle.MicroKernel.DefaultKernel.ResolveAll(Type service, IDictionary arguments) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs:line 285

at Castle.Facilities.TypedFactory.TypedFactoryComponentCollection.Resolve(IKernel kernel) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\TypedFactoryComponentCollection.cs:line 39

at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs:line 173

at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs:line 83

at Castle.DynamicProxy.AbstractInvocation.Proceed()

at Castle.Proxies.HandlerFactoryProxy.GetHandlersForEvent(IEvent ev)

at CastleWindsorTests.Program.TryIt(HandlerFactory factory) in c:\users\user\documents\visual studio 2010\Projects

How to implement the HandlerSelector so that it works well with factory defined as returning object[] whereas the real objects at runtime are closed generic types? I'll be happy to be pointed to some existing documentation with guidelines for implementors of ITypedFactoryComponentSelector / DefaultTypedFactoryComponentSelector. Yes, I tried the http://docs.castleproject.org/(S(kwaa14uzdj55gv55dzgf0vui))/Windsor.Typed-Factory-Facility-interface-based-factories.ashx but here's not much about the above types.

I really don't want to introduce a service locator (instead of factory) ;).


To answer my own question:

I must've been blind. After reading more closely the xmldoc of the methods I override, changing HandlerSelector to the following solved the problem:

public class HandlerSelector : DefaultTypedFactoryComponentSelector
{
    protected override TypedFactoryComponent BuildFactoryComponent(MethodInfo method, string componentName, Type componentType, System.Collections.IDictionary additionalArguments)
    {                
        return new TypedFactoryComponentCollection(componentType, additionalArguments);
    }

    protected override Type GetComponentType(MethodInfo method, object[] arguments)
    {
        var message = arguments[0];
        var handlerType = typeof(IHandler<>).MakeGenericType(message.GetType());
        return handlerType;
    }
}


@workabyte

For Castle v3, GetComponentType method stays the same but the BuildFactoryComponent method looks like this for me:

protected override Func<IKernelInternal, IReleasePolicy, object> BuildFactoryComponent(MethodInfo method, string componentName, Type componentType, IDictionary additionalArguments)
{
    return (kernel, rp) => kernel.ResolveAll(componentType);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜