2012-05-29 30 views
1

注意:在開始之前請注意,在將ICommandHandler的定義更改爲包含多個通用約束之前,此工作完美無缺,其中一個約束可正常工作。Castle Windsor輸入工廠,綁定多個通用約束?

但是,我似乎沒有得到多個約束傳遞到選擇器的「參數」數組。我錯過了什麼嗎?

這就是所謂的有:

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

Selector類:

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

溫莎安裝程序文件:

  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>())); 
+0

請問您可以將'HandlerResolver'添加到您的問題中嗎?我遇到類似的問題,看到你的HandlerResolver –

+0

@danrichardson是否找到HandlerResolver?我有同樣的問題。我會很感激,如果你幫我 – Ehsan

+0

我不記得了,對不起。根據答案,我認爲這更像是一個編程錯誤,而不是城堡內部的問題。 –

回答

2

通常情況下,以一致的方式編寫問題通常會導致答案。

改變選擇代碼:

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

解決了這個問題。