Esempio n. 1
0
        /// <summary>
        /// Evaluates the rules specified by the rule set descriptor.
        /// </summary>
        /// <typeparam name="TRule">The type of the rule.</typeparam>
        /// <typeparam name="TAggregationResult">The type of the aggregation result.</typeparam>
        /// <param name="ruleSetDescriptor">The rule set descriptor describing which rules have to be evaluated.</param>
        /// <returns>The result of the validation.</returns>
        public TAggregationResult Evaluate <TRule, TAggregationResult>(IRuleSetDescriptor <TRule, TAggregationResult> ruleSetDescriptor)
        {
            IRuleSet <TRule> ruleSet = ruleSetDescriptor.Factory.CreateRuleSet();

            ICollection <IRulesProvider> providers = this.rulesProviderFinder.FindRulesProviders(ruleSetDescriptor);

            foreach (IRulesProvider rulesProvider in providers)
            {
                IRuleSet <TRule> rs = rulesProvider.GetRules(ruleSetDescriptor);
                if (rs != null)
                {
                    ruleSet.AddRange(rs);
                }
            }

            string             logInfo;
            TAggregationResult result = ruleSetDescriptor.Aggregator.Aggregate(ruleSet, out logInfo);

            string logMessage = string.Format(
                CultureInfo.InvariantCulture,
                "Validated rule set '{0}' with rules from rules providers '{1}'. Validated Rules: '{2}'.",
                ruleSetDescriptor,
                FormatHelper.ConvertToString(providers, ", "),
                logInfo);

            log.DebugFormat(
                CultureInfo.InvariantCulture,
                logMessage);

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Returns the rules described by the specified rule set descriptor
        /// by calling the corresponding provider method of the derived class.
        /// See remarks of class description for more details.
        /// </summary>
        /// <typeparam name="TRule">The type of the rule.</typeparam>
        /// <typeparam name="TAggregationResult">The type of the aggregation result.</typeparam>
        /// <param name="ruleSetDescriptor">The rule set descriptor.</param>
        /// <returns>A set of rules to be validated.</returns>
        public virtual IRuleSet <TRule> GetRules <TRule, TAggregationResult>(IRuleSetDescriptor <TRule, TAggregationResult> ruleSetDescriptor)
        {
            IRuleSet <TRule> result = null;

            var delegates = from item in this.mapping
                            where item.Key.IsAssignableFrom(ruleSetDescriptor.GetType())
                            select item.Value;

            if (delegates.Count() > 0)
            {
                foreach (Delegate d in delegates)
                {
                    var resultSet = (IRuleSet <TRule>)d.DynamicInvoke(ruleSetDescriptor);
                    if (result == null)
                    {
                        result = resultSet;
                    }
                    else
                    {
                        result.AddRange(resultSet);
                    }
                }
            }
            else
            {
                Log.DebugFormat(
                    "RulesProvider '{0}' does not provide rules for ruleSetDescriptor '{1}'.",
                    GetType().FullName,
                    ruleSetDescriptor.GetType().FullName);
            }

            return(result);
        }