5

I'm using Jimmy Bogard's Mediatr and trying to use the pipleine example here

my problem is that although I can get all my closing generic types like so

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

I can't decorate them all with the MediatorPipeline.

So if I were using StructureMap I could use something like this

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

I can't find how I would achieve it with Ninject so that when when my Mediator is called it uses the Mediator pipeline and then down to the original Handler

Kev Hunter
  • 2,565
  • 4
  • 25
  • 39
  • 2
    care to post your answer yourself? https://github.com/MrKevHunter/RedisMediatorClient – BatteryBackupUnit Jan 12 '15 at 11:46
  • Did you ever find a solution for this? I want to know how to do this for a pattern like https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=91, where I want to be able to register one or more decorators for my `ICommandHandler` generics. – xan Dec 10 '15 at 16:48
  • it's in the github repo above – Kev Hunter Dec 11 '15 at 09:57

1 Answers1

2

Couple of ways to do this. You can either do a convention based scan which you are already doing, and add a contextual binding to the end of it:

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

Then just do the exact same thing again without the WhenInjectedExactlyInto context filter:

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

This would require two assembly scans to happen though.

Another way would be to write an IBindingGenerator, and perform multiple bindings in there - one with WhenInjectedExactlyInto and another without. This would then only require a single convention based binding using the .BindWith<MyBindingGenerator>() syntax instead of .BindAllInterfaces()

AaronHS
  • 1,334
  • 12
  • 29