2015-01-09 29 views
4

實現特定的接口我使用吉米博加德的Mediatr並嘗試使用pipleine例如here我如何裝點我的所有綁定在Ninject

我的問題是,雖然我可以得到我所有的關閉泛型類型,像這樣

 kernel.Bind(
      x => 
       x.FromAssemblyContaining<ExpensiveRequest>() 
        .SelectAllClasses() 
        .InheritedFrom(typeof (IRequestHandler<,>)).BindAllInterfaces() 

我不能用MediatorPipeline來裝飾它們。

所以,如果我用StructureMap我可以用這樣的

cfg.For(typeof(IRequestHandler<,>)).DecorateAllWith(typeof(MediatorPipeline<,>)); 

我找不到我如何與Ninject實現它這樣當我的中保被稱爲它使用的調解管道,然後一直到原來的Handler

+2

謹慎地發佈您自己的答案? https://github.com/MrKevHunter/RedisMediatorClient – BatteryBackupUnit 2015-01-12 11:46:53

+0

你有沒有找到這個解決方案?我想知道如何爲像https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91這樣的模式執行此操作,我希望能夠爲我的註冊一個或多個裝飾器ICommandHandler '泛型。 – xan 2015-12-10 16:48:28

+0

它在上面的github回購中 – 2015-12-11 09:57:27

回答

1

幾種方法來做到這一點。你可以做一個基於常規掃描你已經這樣做,並添加上下文結合到它的結束:

kernel.Bind(x => x.FromAssemblyContaining<ExpensiveRequest>() 
    .SelectAllClasses() 
    .InheritedFrom(typeof(IRequestHandler<,>)) 
    .BindAllInterfaces(); 
    .Configure(c => c.WhenInjectedExactlyInto(typeof(MediatorPipeline<,>)); 

然後,只需做同樣的事情再次沒有WhenInjectedExactlyInto上下文過濾器:

kernel.Bind(x => x.FromAssemblyContaining<ExpensiveRequest>() 
    .SelectAllClasses() 
    .InheritedFrom(typeof(IRequestHandler<,>)) 
    .BindAllInterfaces(); 

雖然這需要兩次組裝掃描。

另一種方法是編寫IBindingGenerator,並在其中執行多個綁定 - 一個是WhenInjectedExactlyInto,另一個沒有。這將只需要使用.BindWith<MyBindingGenerator>()語法而不是.BindAllInterfaces()