注: 始める前に、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>()));