Ejemplo n.º 1
0
        /// <summary>
        /// Defines a condition that applies to several rules
        /// </summary>
        /// <param name="predicate">The condition that should apply to multiple rules</param>
        /// <param name="action">Action that encapsulates the rules.</param>
        /// <returns></returns>
        public IConditionBuilder When(Func <T, bool> predicate, Action action)
        {
            var propertyRules = new List <IValidationRule>();

            using (_rules.OnItemAdded(propertyRules.Add)) {
                action();
            }

            // Generate unique ID for this shared condition.
            var id = "_FV_Condition_" + Guid.NewGuid();

            bool Condition(ValidationContext context)
            {
                string cacheId = null;

                if (context.InstanceToValidate != null)
                {
                    cacheId = id + context.InstanceToValidate.GetHashCode();

                    if (context.RootContextData.TryGetValue(cacheId, out var value))
                    {
                        if (value is bool result)
                        {
                            return(result);
                        }
                    }
                }

                var executionResult = predicate((T)context.InstanceToValidate);

                if (context.InstanceToValidate != null)
                {
                    context.RootContextData[cacheId] = executionResult;
                }
                return(executionResult);
            }

            // Must apply the predicate after the rule has been fully created to ensure any rules-specific conditions have already been applied.
            foreach (var rule in propertyRules)
            {
                //TODO for FV 9 remove explicit reference to CollectionPropertyRule.
                if (rule is PropertyRule p)
                {
                    p.ApplySharedCondition(Condition);
                }
                else
                {
                    throw new NotSupportedException("Cannot call the root-level When/Unless methods on rules that don't inherit from PropertyRule");
                }
            }

            return(new ConditionOtherwiseBuilder(_rules, Condition));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Defines an asynchronous condition that applies to several rules
        /// </summary>
        /// <param name="predicate">The asynchronous condition that should apply to multiple rules</param>
        /// <param name="action">Action that encapsulates the rules.</param>
        /// <returns></returns>
        public IConditionBuilder WhenAsync(Func <T, CancellationToken, Task <bool> > predicate, Action action)
        {
            var propertyRules = new List <IValidationRule>();

            using (_rules.OnItemAdded(propertyRules.Add)) {
                action();
            }

            // Generate unique ID for this shared condition.
            var id = "_FV_AsyncCondition_" + Guid.NewGuid();

            async Task <bool> Condition(ValidationContext context, CancellationToken ct)
            {
                string cacheId = null;

                if (context.InstanceToValidate != null)
                {
                    cacheId = id + context.InstanceToValidate.GetHashCode();

                    if (context.RootContextData.TryGetValue(cacheId, out var value))
                    {
                        if (value is bool result)
                        {
                            return(result);
                        }
                    }
                }

                var executionResult = await predicate((T)context.InstanceToValidate, ct);

                if (context.InstanceToValidate != null)
                {
                    context.RootContextData[cacheId] = executionResult;
                }
                return(executionResult);
            }

            foreach (var rule in propertyRules)
            {
                //TODO for FV 9 remove explicit reference to CollectionPropertyRule.
                if (rule is PropertyRule p)
                {
                    p.ApplySharedAsyncCondition(Condition);
                }
                else
                {
                    throw new NotSupportedException("Cannot call the root-level When/Unless methods on rules that don't inherit from PropertyRule");
                }
            }

            return(new AsyncConditionOtherwiseBuilder(_rules, Condition));
        }
        public virtual void Otherwise(Action action)
        {
            var propertyRules = new List <IValidationRule>();

            Action <IValidationRule> onRuleAdded = propertyRules.Add;

            using (_rules.OnItemAdded(onRuleAdded)) {
                action();
            }

            foreach (var rule in propertyRules)
            {
                rule.ApplySharedAsyncCondition(async(ctx, ct) => !await _condition(ctx, ct));
            }
        }
        /// <summary>
        /// Defines a condition that applies to several rules
        /// </summary>
        /// <param name="predicate">The condition that should apply to multiple rules</param>
        /// <param name="action">Action that encapsulates the rules.</param>
        /// <returns></returns>
        public IConditionBuilder When(Func <T, ValidationContext <T>, bool> predicate, Action action)
        {
            var propertyRules = new List <IValidationRule>();

            using (_rules.OnItemAdded(propertyRules.Add)) {
                action();
            }

            // Generate unique ID for this shared condition.
            var id = "_FV_Condition_" + Guid.NewGuid();

            bool Condition(ValidationContext context)
            {
                string cacheId = null;

                if (context.InstanceToValidate != null)
                {
                    cacheId = id + context.InstanceToValidate.GetHashCode();

                    if (context.RootContextData.TryGetValue(cacheId, out var value))
                    {
                        if (value is bool result)
                        {
                            return(result);
                        }
                    }
                }

                var executionResult = predicate((T)context.InstanceToValidate, ValidationContext <T> .GetFromNonGenericContext(context));

                if (context.InstanceToValidate != null)
                {
                    context.RootContextData[cacheId] = executionResult;
                }
                return(executionResult);
            }

            // Must apply the predicate after the rule has been fully created to ensure any rules-specific conditions have already been applied.
            foreach (var rule in propertyRules)
            {
                rule.ApplySharedCondition(Condition);
            }

            return(new ConditionOtherwiseBuilder(_rules, Condition));
        }
        /// <summary>
        /// Defines an asynchronous condition that applies to several rules
        /// </summary>
        /// <param name="predicate">The asynchronous condition that should apply to multiple rules</param>
        /// <param name="action">Action that encapsulates the rules.</param>
        /// <returns></returns>
        public IConditionBuilder WhenAsync(Func <T, ValidationContext <T>, CancellationToken, Task <bool> > predicate, Action action)
        {
            var propertyRules = new List <IValidationRule>();

            using (_rules.OnItemAdded(propertyRules.Add)) {
                action();
            }

            // Generate unique ID for this shared condition.
            var id = "_FV_AsyncCondition_" + Guid.NewGuid();

            async Task <bool> Condition(ValidationContext context, CancellationToken ct)
            {
                string cacheId = null;

                if (context.InstanceToValidate != null)
                {
                    cacheId = id + context.InstanceToValidate.GetHashCode();

                    if (context.RootContextData.TryGetValue(cacheId, out var value))
                    {
                        if (value is bool result)
                        {
                            return(result);
                        }
                    }
                }

                var executionResult = await predicate((T)context.InstanceToValidate, ValidationContext <T> .GetFromNonGenericContext(context), ct);

                if (context.InstanceToValidate != null)
                {
                    context.RootContextData[cacheId] = executionResult;
                }
                return(executionResult);
            }

            foreach (var rule in propertyRules)
            {
                rule.ApplySharedAsyncCondition(Condition);
            }

            return(new AsyncConditionOtherwiseBuilder(_rules, Condition));
        }
Ejemplo n.º 6
0
        public virtual void Otherwise(Action action)
        {
            var propertyRules = new List <IValidationRule>();

            Action <IValidationRule> onRuleAdded = propertyRules.Add;

            using (_rules.OnItemAdded(onRuleAdded)) {
                action();
            }

            foreach (var rule in propertyRules)
            {
                if (rule is PropertyRule p)
                {
                    p.ApplySharedAsyncCondition(async(ctx, ct) => !await _condition(ctx, ct));
                }
                else
                {
                    throw new NotSupportedException("Cannot call the root-level When/Unless methods on rules that don't inherit from PropertyRule");
                }
            }
        }