我正在尝试用装饰器包装我的通用接口,但它根本不起作用,在我看来,从其他问题来看,唯一的方法是为每个装饰器明确地这样做,我的问题是它是否可能用ninject中的特定装饰器包装实现某个接口的所有类型。
代码:
static void BindMediatr(IKernel kernel) {
kernel.Components.Add < IBindingResolver, ContravariantBindingResolver > ();
kernel.Bind(scan => scan.FromAssemblyContaining < IMediator > ()
.SelectAllClasses()
.BindDefaultInterface());
kernel.Bind < SingleInstanceFactory > ().ToMethod(ctx => t => ctx.Kernel.Get(t));
kernel.Bind < MultiInstanceFactory > ().ToMethod(ctx => t => ctx.Kernel.GetAll(t));
kernel.Bind(
x => x.FromThisAssembly()
.SelectAllClasses()
.InheritedFromAny(typeof(IAsyncRequestHandler < , > ))
.BindAllInterfaces());
kernel.Bind(typeof(IAsyncRequestHandler < , > ))
.To(typeof(Decorater < , > ))
.WhenInjectedInto < ApiController > ();
}
public class Decorater < TRequest, TResponse >
: IAsyncRequestHandler < TRequest, TResponse >
where TRequest: IAsyncRequest < TResponse > {
IAsyncRequestHandler < TRequest,
TResponse > _decoratee;
public Decorater(IAsyncRequestHandler < TRequest, TResponse > decoratee) {
_decoratee = decoratee;
}
public Task < TResponse > Handle(TRequest message) {
// do something here
}
}