温莎城堡类型化工厂设施 [英] Castle Windsor Typed Factory Facility with generics

查看:49
本文介绍了温莎城堡类型化工厂设施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.

我尝试了KrzysztofKoźmic描述的方法< a href = http://kozmic.pl/2010/03/11/advanced-castle-windsor-ndash-generic-typed-factories-auto-release-and-more/ rel = nofollow> http:/ /kozmic.pl/2010/03/11/advanced-castle-windsor-ndash-generic-typed-factories-auto-release-and-more/
但仍然存在问题。
基本上,我的问题归结为从DefaultTypedFactoryComponentSelector派生的自定义类型返回什么类型。

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

温莎安装程序定义为:

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);我收到一个抱怨数组类型不匹配的异常:
试图以与数组不兼容的类型访问元素。

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."

堆栈跟踪:

在System.Collections.Generic.Dictionary 2.ValueCollection.CopyTo(TValue [] array,Int32 index)
在系统处
。 Collections.Generic.Dictionary
2.ValueCollection.System.Collections.ICollection.CopyTo(Array array,Int32 index)

at Castle.MicroKernel.DefaultKernel.ResolveAll(类型服务,IDictionary参数))在e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs:line 285

中Castle.Facilities.TypedFactory.TypedFactoryComponentCollection中。在Castle中的e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\TypedFactoryComponentCollection.cs:line 39

中解析(IKernel内核)。在e:\OSS.Code\Castle.Windsor\src\Cast中的Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation调用) le.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs:行173

在e:\OSS中的Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation调用)中。 Code\Castle.Windsor\src\Castle.Windsor\设施\TypedFactory\内部\TypedFactoryInterceptor.cs:第83行

at Castle.DynamicProxy.AbstractInvocation.Proceed()
Castle.Proxies.HandlerFactoryProxy.GetHandlersForEvent(IEvent ev)的
在CastleWindsorTests.Program.TryIt(HandlerFactory factory)的
在c:\users\user\documents\visual studio中2010\Projects

at System.Collections.Generic.Dictionary2.ValueCollection.CopyTo(TValue[] array, Int32 index)
at System.Collections.Generic.Dictionary
2.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

如何实现HandlerSelector,使其与工厂定义为返回对象[]的效果很好,而运行时的实际对象是封闭的泛型类型?
很高兴地指向一些现有文档,以及有关ITypedFactoryComponentSelector / DefaultTypedFactoryComponentSelector的实现者的准则。是的,我尝试了 http ://docs.castleproject.org/(S(kwaa14uzdj55gv55dzgf0vui))/Windsor.Typed-Factory-Facility-interface-based-factories.ashx ,但这与上述类型无关。

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) ;).

推荐答案

要回答我自己的问题:

我一定是瞎子。在更仔细地阅读了我重写的方法的xmldoc之后,将HandlerSelector更改为以下内容可以解决问题:

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

这篇关于温莎城堡类型化工厂设施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆