/// <summary>
 /// Initializes a new instance of the <see cref="RuleEvaluatedEventArgs"/> class.
 /// </summary>
 /// <param name="ruleEvaluation">The rule evaluation.</param>
 public RuleEvaluatedEventArgs(RuleEvaluation ruleEvaluation)
 {
     if (ruleEvaluation == null)
     {
         throw new ArgumentNullException(nameof(ruleEvaluation));
     }
     RuleEvaluation = ruleEvaluation;
 }
Exemplo n.º 2
0
        public override string GetMessage(RuleEvaluation evaluation)
        {
            var successfulEvaluation = evaluation as SuccessfulEvaluation ?? new SuccessfulEvaluation();
            var template             = buildMessage(successfulEvaluation);

            return(evaluation == null
                       ? template
                       : MessageGenerator.Detokenize(template, successfulEvaluation.Parameters));
        }
        public override string GetMessage(RuleEvaluation evaluation)
        {
            var successfulEvaluation = evaluation as SuccessfulEvaluation ?? new SuccessfulEvaluation();
            var template             = buildMessage(successfulEvaluation,
                                                    successfulEvaluation.Target
                                                    .IfTypeIs <TTarget>()
                                                    .ElseDefault());

            return(MessageGenerator.Detokenize(template, successfulEvaluation.Parameters));
        }
Exemplo n.º 4
0
        public override string GetMessage(RuleEvaluation evaluation)
        {
            var failedEvaluation = evaluation as FailedEvaluation ?? new FailedEvaluation();

            var template = buildMessage(failedEvaluation);

            return(evaluation == null
                       ? template
                       : MessageGenerator.Detokenize(template, failedEvaluation.Parameters));
        }
Exemplo n.º 5
0
        internal virtual void AddEvaluation(RuleEvaluation evaluation)
        {
            if (disposed)
            {
                return;
            }

            evaluations.Add(evaluation);

            NotifyRuleEvaluated(evaluation);
        }
        /// <summary>
        ///   Builds a message describing the <see cref="RuleEvaluation" /> .
        /// </summary>
        /// <param name="evaluation"> The rule evaluation for which to build a message. </param>
        /// <returns> A message describing the evaluation. </returns>
        private static string BuildMessage(RuleEvaluation evaluation)
        {
            MessageContext.SetCurrentRuleEvaluation(evaluation);

            var parts = new List <string>();

            if (evaluation.Target != null)
            {
                parts.Add($"Target: {evaluation.Target}");
            }

            if (evaluation.Parameters != null)
            {
                parts.AddRange(evaluation.Parameters.Select(parameter => $"{parameter.Key}: {parameter.Value}"));
            }

            // add details about any extensions
            // reflecting out the field bypasses the requirement to know the rule's generic type
            var extensionsField = evaluation.Rule?
                                  .GetType()
                                  .GetField("extensions",
                                            BindingFlags.NonPublic |
                                            BindingFlags.Instance);

            if (extensionsField != null)
            {
                var extensions = extensionsField.GetValue(evaluation.Rule) as Dictionary <Type, object>;
                if (extensions != null)
                {
                    foreach (var extension in extensions)
                    {
                        // extension.Key is the Type
                        // the extension class should have a meaningful ToString override
                        if ((extension.Value is SuccessMessageTemplate) && evaluation is FailedEvaluation)
                        {
                            continue;
                        }

                        if ((extension.Value is FailureMessageTemplate) && evaluation is SuccessfulEvaluation)
                        {
                            continue;
                        }

                        parts.Add("Extension: " + extension.Key + ": " + extension.Value);
                    }
                }

                parts.Add("Rule: " + evaluation.Rule);
            }

            return(string.Join(" / ", parts.ToArray()));
        }
        /// <summary>
        ///   Gets the message for the specified <see cref="RuleEvaluation" /> .
        /// </summary>
        /// <param name="evaluation"> The <see cref="RuleEvaluation" /> . </param>
        /// <returns> A validation message <see cref="string" /> . </returns>
        public string GetMessage(RuleEvaluation evaluation)
        {
            if (evaluation.Rule != null)
            {
                var failure = evaluation as FailedEvaluation;
                if (!string.IsNullOrEmpty(failure?.ErrorCode))
                {
                    if (messages.ContainsKey(failure.ErrorCode))
                    {
                        return(messages[failure.ErrorCode]);
                    }
                }

                if (!string.IsNullOrEmpty(evaluation.MessageTemplate) && evaluation.Parameters != null)
                {
                    return(MessageGenerator.Detokenize(evaluation.MessageTemplate, evaluation.Parameters));
                }
            }

            return(BuildMessage(evaluation));
        }
Exemplo n.º 8
0
 public virtual string GetMessage(RuleEvaluation evaluation) => value;
Exemplo n.º 9
0
        private void NotifyRuleEvaluated(RuleEvaluation evaluation)
        {
            RuleEvaluated?.Invoke(this, new RuleEvaluatedEventArgs(evaluation));

            parent?.NotifyRuleEvaluated(evaluation);
        }
Exemplo n.º 10
0
 internal static void SetCurrentRuleEvaluation(RuleEvaluation evaluation) =>
 CallContext.LogicalSetData(RuleEvaluationKey, evaluation);