Question

Note: Before I begin please note this worked perfectly before the definition of ICommandHandler was changed to include multiple generic constraints, with one constraint it works fine.

However, I do not seem to be getting multiple constraints passed into the "arguments" array of the selector. Am I missing something?

This is called with:

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

The Factory interface:

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

The Selector class:

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

The Windsor installer file:

           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>()));
Was it helpful?

Solution

As is often the case, writing the problem in a coherent manner often leads to an answer.

Changing the selector code to:

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

Solves the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top