We use Ninjects-based convention bindings to automatically associate a set of commands and queries with their handlers. So far, we have one decorator working using the following.
Bind all without an attribute:
Kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses() .InheritedFrom(typeof(ICommandHandler<>)) .WithoutAttribute<DoCheckAttribute>() .BindAllInterfaces());
Bind all attribute:
Kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses() .InheritedFrom(typeof(ICommandHandler<>)) .WithAttribute<DoCheckAttribute>() .BindAllInterfaces() .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));
We tried the following to add another decorator, however this fails.
Bind all without an attribute:
Kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses() .InheritedFrom(typeof(ICommandHandler<>)) .WithoutAttribute<DoCheckAttribute>() .WithoutAttribute<DoOtherCheckAttribute>() .BindAllInterfaces());
Bind all attribute:
Kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses() .InheritedFrom(typeof(ICommandHandler<>)) .WithAttribute<DoCheckAttribute>() .WithoutAttribute<DoOtherCheckAttribute>() .BindAllInterfaces() .Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>)))); Kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses() .InheritedFrom(typeof(ICommandHandler<>)) .WithoutAttribute<DoCheckAttribute>() .WithAttribute<DoOtherCheckAttribute>() .BindAllInterfaces() .Configure(c => c.WhenInjectedInto(typeof(DoOtherCheckDecorator<>))));
Is it possible to achieve this this way using Ninject? Do I need to return to the definition of each binding manually?.?
Bind<X>.To<Y>.WhenInjectedInto(?)
Ideally, we would use syntax, for example:
Bind<X>.To<Y>.WithDecorator<Z>.When(a => a.HasAttribute<DoCheckAttribute>)
source share