示例#1
0
        /// <summary>
        /// Strong typed rule invocation.
        /// </summary>
        /// <typeparam name="TParameter">The type of the rule parameter.</typeparam>
        /// <param name="rule">The rule to be evaluated.</param>
        /// <param name="ruleParameter">The parameter for invoking the rule.</param>
        /// <param name="valueName">The name of the data to be checked.</param>
        /// <returns>True if the rule check is ok, false if the rule is violated.</returns>
        public bool ExecuteRuleExpression <TParameter>(RuleBase <TData, TParameter> rule, TParameter ruleParameter, string valueName)
        {
            // if there is no rule, we cannot say that the rule is validated
            if (rule == null)
            {
                return(true);
            }

            // let the concrete execution class decide if we want to execute the expression
            if (!this.BeforeInvoke(rule, ruleParameter, valueName))
            {
                return(true);
            }

            var validationResult = false;

            try
            {
                // execute the expression
                validationResult = rule.CheckExpression(this.Value, ruleParameter);
            }
            catch (NullReferenceException)
            {
            }
            catch (Exception ex)
            {
                if (!this.HandleInvokeException(ex, rule, ruleParameter, valueName))
                {
                    throw;
                }
            }

            var ruleType = rule.GetType();
            var result   = new RuleValidationResult(
                ruleType,
                string.Format(
                    CultureInfo.CurrentCulture,
                    Resources.RuleValidationResultStandardMessage,
                    ruleType.Namespace + "." + ruleType.Name,
                    valueName,
                    string.Format(CultureInfo.CurrentCulture, rule.Message, ruleParameter, valueName)),
                valueName,
                validationResult);

            foreach (var action in Bouncer.GetAfterInvokeActions())
            {
                action.Invoke(result);
            }

            this.AfterInvoke(result);

            if (this.PreviousExecuter != null)
            {
                this.PreviousExecuter.AssertAll();
            }

            return(validationResult);
        }