Exemplo n.º 1
0
        /// <summary>
        ///   Determines whether the specified target is valid.
        /// </summary>
        /// <param name="target"> The target. </param>
        /// <param name="scope"> The ValidationScope for the operation. </param>
        /// <returns> <c>true</c> if the specified target is valid; otherwise, <c>false</c> . </returns>
        public virtual bool Check(TTarget target, ValidationScope scope)
        {
            // when there is no active ValidationScope, simply perform the check
            if (scope == null)
            {
                if (preconditions != null && preconditions.Any(rule => !rule.Check(target)))
                {
                    // this rule is considered to have succeeded, i.e. not failed, because it has not been evaluated, so return true:
                    return(true);
                }
                return(PerformCheck(target, scope));
            }

            if (CheckPreconditions(scope.Parent ?? scope, target))
            {
                return(true);
            }

            // get the result of the rule
            var result = PerformCheck(target, scope);

            var messageGenerator = MessageGenerator ?? scope.MessageGenerator;

            // extract paramaters from the scope and store them if needed in a FailedEvaluation
            var parameters = scope.FlushParameters();

            if (!result)
            {
                // record the failure in the ValidationScope
                scope.AddEvaluation(new FailedEvaluation(target, this, messageGenerator)
                {
                    IsInternal = CreatesEvaluationsAsInternal,
                    Parameters = parameters
                });
            }
            else
            {
                scope.AddEvaluation(new SuccessfulEvaluation(target, this, messageGenerator)
                {
                    IsInternal = CreatesEvaluationsAsInternal,
                    Parameters = parameters
                });
            }

            return(result);
        }
Exemplo n.º 2
0
        private void RecordResult(ValidationScope scope, bool result, TTarget target)
        {
            var parameters = scope.FlushParameters();

            var messageGenerator = scope.MessageGenerator;

            if (result)
            {
                scope.AddEvaluation(new SuccessfulEvaluation(target, this)
                {
                    MessageGenerator = messageGenerator,
                    Parameters       = parameters
                });
            }
            else
            {
                scope.AddEvaluation(new FailedEvaluation(target, this)
                {
                    MessageGenerator = messageGenerator,
                    Parameters       = parameters
                });
            }
        }