/// <summary> /// Invokes a rule for each item in the collection /// </summary> /// <typeparam name="TProperty">Type of property</typeparam> /// <param name="expression">Expression representing the collection to validate</param> /// <returns>An IRuleBuilder instance on which validators can be defined</returns> public IRuleBuilderInitial <T, TProperty> RuleForEach <TProperty>(Expression <Func <T, IEnumerable <TProperty> > > expression) { expression.Guard("Cannot pass null to RuleForEach"); var rule = CollectionPropertyRule <TProperty> .Create(expression, () => CascadeMode); AddRule(rule); var ruleBuilder = new RuleBuilder <T, TProperty>(rule); return(ruleBuilder); }
public void ThenClause_WithRuleCollection_CollectionRulesAreRun() { var rule = CollectionPropertyRule.CreateCollectionPropertyRule <Patient, IEnumerable <Address> > ( p => p.Addresses, "Rule 1"); var addressRuleCollection = new AddressRuleCollection(); var ruleCalled = false; addressRuleCollection.NewRule(() => addressRuleCollection.MyEmptyRule).When(c => ruleCalled = true); rule.WithRuleCollection(addressRuleCollection); var patient = new Patient(); patient.Addresses.Add(new Address(null, null, null, null, null)); var ruleEngineContext = new RuleEngineContext <Patient> (patient); foreach (var thenClause in rule.ThenClauses) { thenClause(ruleEngineContext); } Assert.IsTrue(ruleCalled); }
public void WithRuleCollection_WithNullRuleCollection_ThrowsArgumentException() { var rule = CollectionPropertyRule.CreateCollectionPropertyRule <Customer, string> (c => c.FirstName, "Rule 1"); rule.WithRuleCollection <Customer> (null); }
public void CreateCollectionPropertyRule_WithPropertyExpression_ReturnsPropertyRule() { var rule = CollectionPropertyRule.CreateCollectionPropertyRule <Customer, string> (c => c.FirstName, "Rule 1"); Assert.IsInstanceOfType(rule, typeof(CollectionPropertyRule)); }
public void CreateCollectionPropertyRule_WithNullName_ThrowsArgumentException() { CollectionPropertyRule.CreateCollectionPropertyRule <Customer, string> (c => c.FirstName, null); }
public void CreateCollectionPropertyRule_WithNullPropertyExpression_ThrowsArgumentException() { CollectionPropertyRule.CreateCollectionPropertyRule <Customer, string> (null, "Rule 1"); }