我有以下界面...
public interface IHandler<in TFor> where TFor : IRequest
{
void Handle(IEnumerable<TFor> requests);
}
这通常是这样实现的......
public class AssignmentHandler : HandlerBase, IHandler<AssignmentRequest>
{
public void Handle(IEnumerable<AssignmentRequest> assigmentRequests)
{
foreach(var request in assignmentRequests).....
}
}
IRequest
只是一个标记界面(此时)。
我通过以下约定注册所有处理程序...
public override void Load()
{
Kernel.Bind(x => x.FromAssemblyContaining<IHandler>()
.SelectAllClasses()
.InheritedFrom(typeof(IHandler<>))
.BindAllInterfaces());
//i also tried...
Kernel.Bind(x => x.FromAssemblyContaining<IHandler>()
.SelectAllClasses()
.InheritedFrom<IHandler<IRequest>>()
.BindSingleInterface());
}
他们单独解决就好了。
在一种情况下,我想解析所有处理程序,并将它们注入到这样的构造函数中......
public SomeConstructor(IEnumerable<IHandler<IRequest>> allHandlers)
这不起作用并且总是返回空。
我的理解是因为我按照惯例将它们注册为IHandler<ConcreteRequest>
,而不是IHandler<IRequest>
2 个不同的签名。
我如何按照惯例注册所有处理程序,以便将它们共同识别为IEnumerable<IHandler<IRequest>>
同时也单独识别?
第二次注册是可以的,但更喜欢通过两个签名解决一个实现。