Exemplo n.º 1
0
 /// <summary>
 /// Adds a attribute to the rule with the key "PropertyChain".
 /// </summary>
 /// <typeparam name="TContext">The type of the context.</typeparam>
 /// <typeparam name="TSubject">The type of the subject.</typeparam>
 /// <typeparam name="TProperty">The type of the property.</typeparam>
 /// <param name="ruleBuilder">The rule builder.</param>
 /// <param name="propertyExpression">The property expression.</param>
 /// <returns>A <see cref="IRuleBuilderInitializer{TContext,TSubject}"/>.</returns>
 public static IRuleBuilderInitializer <TContext, TSubject> RunForProperty <TContext, TSubject, TProperty>(
     this IRuleBuilderInitializer <TContext, TSubject> ruleBuilder,
     Expression <Func <TSubject, TProperty> > propertyExpression)
     where TContext : RuleEngineContext <TSubject>
 {
     ruleBuilder.AddAttribute("PropertyChain", PropertyChain.FromLambdaExpression(propertyExpression));
     return(ruleBuilder);
 }
 /// <summary>
 /// Checks for duplicates.
 /// </summary>
 /// <typeparam name="TContext">The type of the context.</typeparam>
 /// <typeparam name="TSubject">The type of the subject.</typeparam>
 /// <param name="ruleBuilder">The rule builder.</param>
 /// <param name="collectionFunc">The collection func.</param>
 /// <returns>A <see cref="IRuleBuilder{TContext,TSubject}"/></returns>
 public static IRuleBuilder <TContext, TSubject> NoDuplicates <TContext, TSubject> (
     this IRuleBuilderInitializer <TContext, TSubject> ruleBuilder, Func <TContext, IEnumerable <TSubject> > collectionFunc)
     where TContext : RuleEngineContext <TSubject>
 {
     return(ruleBuilder.When(
                (s, ctx) =>
     {
         var collection = collectionFunc(ctx);
         bool isDuplicate;
         if (s is IValuesEquatable)
         {
             var aggregateNodeValueObject = (s as IValuesEquatable);
             isDuplicate = collection.Any(item => aggregateNodeValueObject.ValuesEqual(item));
         }
         else
         {
             isDuplicate = collection.Any(item => item.Equals(s));
         }
         return isDuplicate;
     })
            .ThenReportRuleViolation((s, ctx) => string.Format("Cannot not have duplicate {0}.", ctx.NameProvider.GetName(s))));
 }