1

注: 始める前に、ICommandHandler の定義が複数の一般的な制約を含むように変更される前に、これが完全に機能していることに注意してください。1 つの制約で問題なく機能します。

ただし、セレクターの「引数」配列に複数の制約が渡されているようには見えません。何か不足していますか?

これは次のように呼び出されます:

   var handler = _factory.GetHandlerForCommand<TCommand, TResult>(command);

工場インターフェース:

    public interface ICommandHandlerFactory
    {
        ICommandHandler<TCommand, TResult> GetHandlerForCommand<TCommand, TResult>(ICommand command) 
            where TCommand : class, ICommand 
            where TResult : IDTOBase;
    }

セレクタークラス:

    public class HandlerSelector : DefaultTypedFactoryComponentSelector 
    {
        protected override Func<Castle.MicroKernel.IKernelInternal, Castle.MicroKernel.IReleasePolicy, object> BuildFactoryComponent(System.Reflection.MethodInfo method, string componentName, Type componentType, System.Collections.IDictionary additionalArguments)
        {
            return new HandlerResolver(componentName,
                                                    componentType,
                                                    additionalArguments,
                                                    FallbackToResolveByTypeIfNameNotFound,
                                                    GetType()).Resolve;
        }

        protected override string GetComponentName(System.Reflection.MethodInfo method, object[] arguments)
        {
            return null;
        }

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

Windsor インストーラー ファイル:

           container
                .Register(
                    Component
                        .For<HandlerSelector>()
                        .ImplementedBy<HandlerSelector>(),
                    AllTypes
                        .FromAssemblyContaining<ICommandHandlerFactory>()
                            .BasedOn(typeof(ICommandHandler<,>))
                            .WithService.Base()
                            .Configure(c => c.LifeStyle.Is(LifestyleType.PerWebRequest)),
                    Component
                        .For<ICommandHandlerFactory>()


                .AsFactory(c => c.SelectedWith<HandlerSelector>()));
4

1 に答える 1

2

よくあることですが、首尾一貫した方法で問題を書くことはしばしば答えにつながります。

セレクターコードを次のように変更します。

var genericArgs = method.GetGenericArguments();
var handlerType = typeof(ICommandHandler<,>).MakeGenericType(genericArgs[0], genericArgs[1]);
return handlerType;

問題を解決します。

于 2012-05-29T15:37:33.993 に答える