城堡温莎型工厂设施与泛型

时间:2011-05-10 22:40:05

标签: dependency-injection castle-windsor

我正在尝试注册一个可以解析定义如下的事件处理程序数组的工厂:

    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 [],因为在运行时我不知道将返回哪些封闭的泛型类型。

我尝试了KrzysztofKoźmichttp://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);我得到一个例外抱怨数组类型不匹配: “尝试将元素作为与数组不兼容的类型进行访问。”

堆栈追踪:

at System.Collections.Generic.Dictionary 2.ValueCollection.CopyTo(TValue[] array, Int32 index)
at System.Collections.Generic.Dictionary
2.ValueCollection.System.Collections.ICollection.CopyTo(Array array,Int32 index)
   在Castle.MicroKernel.DefaultKernel.ResolveAll(类型服务,IDictionary参数)中的e:\ OSS.Code \ Castle.Windsor \ src \ Castle.Windsor \ MicroKernel \ DefaultKernel_Resolve.cs:第285行    at Castle.Facilities.TypedFactory.TypedFactoryComponentCollection.Resolve(IKernel kernel)在e:\ OSS.Code \ Castle.Windsor \ src \ Castle.Windsor \ Facilities \ TypedFactory \ TypedFactoryComponentCollection.cs:第39行
   在Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation)中的e:\ OSS.Code \ Castle.Windsor \ src \ Castle.Windsor \ Facilities \ TypedFactory \ Internal \ TypedFactoryInterceptor.cs:第173行
   在E:\ OSS.Code \ Castle.Windsor \ src \ Castle.Windsor \ Facilities \ TypedFactory \ Internal \ TypedFactoryInterceptor.cs中的Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation):第83行    在Castle.DynamicProxy.AbstractInvocation.Proceed()
   在Castle.Proxies.HandlerFactoryProxy.GetHandlersForEvent(IEvent ev)
   在CastleWindsorTests.Program.TryIt(HandlerFactory工厂)的c:\ users \ user \ documents \ visual studio 2010 \ Projects

如何实现HandlerSelector,以便它可以很好地与工厂定义为返回对象[],而运行时的真实对象是封闭的泛型类型? 我很高兴能够指出一些现有的文档,其中包含ITypedFactoryComponentSelector / DefaultTypedFactoryComponentSelector的实现者指南。是的,我尝试了http://docs.castleproject.org/(S(kwaa14uzdj55gv55dzgf0vui))/Windsor.Typed-Factory-Facility-interface-based-factories.ashx,但这里并没有太多关于上述类型。

我真的不想介绍服务定位器(而不是工厂);)。

2 个答案:

答案 0 :(得分:3)

回答我自己的问题:

我一定是个盲人。在仔细阅读了我覆盖的方法的xmldoc后,将HandlerSelector更改为以下内容解决了问题:

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

答案 1 :(得分:1)

@workabyte

对于Castle v3,GetComponentType方法保持不变,但BuildFactoryComponent方法对我来说是这样的:

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