Nous utilisons des liaisons basées sur les conventions Ninjects pour lier automatiquement un ensemble de commandes et de requêtes à leurs gestionnaires. Jusqu'à présent, nous avons un décorateur travaillant en utilisant ce qui suit.Liaisons et décorateurs basés sur les conventions Ninject
Bind tout sans l'attribut:
Kernel.Bind(x =>
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler<>))
.WithoutAttribute<DoCheckAttribute>()
.BindAllInterfaces());
Bind tout avec l'attribut:
Kernel.Bind(x =>
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler<>))
.WithAttribute<DoCheckAttribute>()
.BindAllInterfaces()
.Configure(c => c.WhenInjectedInto(typeof(DoCheckDecorator<>))));
Nous avons essayé ce qui suit pour ajouter un autre décorateur, mais cela échoue.
Bind tout sans l'attribut:
Kernel.Bind(x =>
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom(typeof(ICommandHandler<>))
.WithoutAttribute<DoCheckAttribute>()
.WithoutAttribute<DoOtherCheckAttribute>()
.BindAllInterfaces());
Bind tout avec l'attribut:
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<>))));
Est-il possible d'y parvenir en utilisant de cette manière Ninject? Devons-nous revenir à la définition manuelle de chaque liaison, c'est-à-dire?
Bind<X>.To<Y>.WhenInjectedInto(?)
Idéalement, nous devrions utiliser la syntaxe comme:
Bind<X>.To<Y>.WithDecorator<Z>.When(a => a.HasAttribute<DoCheckAttribute>)
Êtes-vous accro à Ninject? Si vous le souhaitez, je peux vous montrer comment faire avec Simple Injector: avec des décorateurs. Avec Simple Injector, c'est plutôt facile. – Steven