验证规则合同:
public interface IValidationRule
{
bool IsValid();
}
具体验证规则:
public class MyClass : IValidationRule
{
public bool IsValid()
{
return true;
}
}
综合:
public class ValidationRuleComposite : IValidationRule
{
private readonly IEnumerable<IValidationRule> _validationRules;
public ValidationRuleComposite(IEnumerable<IValidationRule> validationRules)
{
_validationRules = validationRules;
}
public bool IsValid()
{
return _validationRules.All(x => x.IsValid());
}
}
当我向容器询问
IValidationRule
时,我想获得ValidationRuleComposite
。如果我要求容器提供IValidationRule
的列表,我想获取除IValidationRule
以外的所有ValidationRuleComposite
实现。如何使用Ninject做到这一点?
最佳答案
首先,您要为IEnumerable 设置绑定,该绑定将被注入到组合中。您可以单独绑定它们:
// Bind all the individual rules for injection into the composite
kernel.Bind<IValidationRule>().To<MyClass>().WhenInjectedInto<ValidationRuleComposite>();
kernel.Bind<IValidationRule>().To<RuleTwo>().WhenInjectedInto<ValidationRuleComposite>();
或者,您也可以使用convention binding extensions轻松设置IEnumerable,这样您就不必为每个具体规则添加单独的绑定。只需确保为复合类添加Exlcuding子句,如下所示:
using Ninject.Extensions.Conventions;
// Bind all the non-composite IValidationRules for injection into ValidationRuleComposite
kernel.Bind(x => x.FromAssemblyContaining(typeof(ValidationRuleComposite))
.SelectAllClasses()
.InheritedFrom<IValidationRule>()
.Excluding<ValidationRuleComposite>()
.BindAllInterfaces()
.Configure(c => c.WhenInjectedInto<ValidationRuleComposite>()));
在我的示例中,复合材料和其他混凝土在同一组件中,但是很显然,如果它们在其他地方,则可以更改约定绑定。
最后,我们需要设置绑定,以便在其他任何地方都需要IValidationRule时,Ninject会提供复合。似乎没有一个优雅的方法可用于此目的,因此我编写了自己的While子句以避免循环注入:
// Now bind the composite to the interface for everywhere except itself
kernel.Bind<IValidationRule>().To<ValidationRuleComposite>()
.When(x => x.Target == null
|| x.Target.Member.ReflectedType != typeof(ValidationRuleComposite));