温莎城堡类型工厂设施与仿制药

发布于 2024-11-06 11:26:38 字数 5947 浏览 1 评论 0原文

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

    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)
在System.Collections.Generic.Dictionary
2.ValueCollection.System.Collections.ICollection.CopyTo(数组数组,Int32索引)
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs 中的 Castle.MicroKernel.DefaultKernel.ResolveAll(类型服务,IDictionary 参数):第 285 行
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\TypedFactoryComponentCollection.cs 中的 Castle.Facilities.TypedFactory.TypedFactoryComponentCollection.Resolve(IKernel kernel):第 39 行
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs 中的 Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInspiration 调用):第 173 行
在 e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Facilities\TypedFactory\Internal\TypedFactoryInterceptor.cs 中的 Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInspiration 调用):第 83 行
在 Castle.DynamicProxy.AbstractInspiration.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但这里对上述类型不多介绍。

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淑女气质 2024-11-13 11:26:38

回答我自己的问题:

我一定是瞎了。在更仔细地阅读了我重写的方法的 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;
    }
}

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;
    }
}
苏璃陌 2024-11-13 11:26:38

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

@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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文