一般的なインタフェースをデコレータでラップしようとしていますが、単に動作しません。これを行う唯一の方法は、デコレータごとに明示的に行うことです。私の質問は、特定のインタフェースを実装しているすべての型を特定のDecoratorとラップすることが可能かどうかです。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
}
}
私は傍受を提供していないよう一定のジェネリック型結果のかもしれません呼び出しアクションの結果を得ることができるようにする必要があります。 –
Xerxes