Esempio n. 1
0
        private TestResult TestSingleValue(object propertyValue, Predicate <ISpecification> filter)
        {
            var result = _innerRules.Test(propertyValue, filter);

            if (result.Fail)
            {
                var message = string.Format(SR.RuleEmbeddeValue, TerminologyTranslator.Translate(this.Property));
                return(new TestResult(false, new TestResultReason(message, result.Reasons)));
            }
            return(new TestResult(true));
        }
Esempio n. 2
0
        /// <summary>
        /// Validates the specified domain object, ignoring any rules that do not satisfy the filter.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="level"></param>
        /// <param name="ruleFilter"></param>
        /// <exception cref="EntityValidationException">Validation failed.</exception>
        private void Validate(DomainObject obj, RuleLevel level, Predicate <ISpecification> ruleFilter)
        {
            var domainClass = obj.GetClass();

            var ruleSets = new List <ISpecification>();

            if (CheckLevel(level, RuleLevel.Low))
            {
                // first check for a cached rule-set
                ValidationRuleSet lowLevelRules;
                if (!_lowLevelRuleSets.TryGetValue(domainClass, out lowLevelRules))
                {
                    // otherwise build it
                    lowLevelRules = IsValidationEnabled(domainClass) ?
                                    ValidationRuleSetCache.GetLowLevelRules(domainClass)
                                                : new ValidationRuleSet();

                    // cache for future use
                    _lowLevelRuleSets.Add(domainClass, lowLevelRules);
                }
                ruleSets.Add(lowLevelRules);
            }

            if (CheckLevel(level, RuleLevel.High))
            {
                // first check for a cached rule-set
                ValidationRuleSet highLevelRules;
                if (!_highLevelRuleSets.TryGetValue(domainClass, out highLevelRules))
                {
                    // otherwise build it
                    highLevelRules = IsValidationEnabled(domainClass) ?
                                     ValidationRuleSetCache.GetHighLevelRules(domainClass)
                                                : new ValidationRuleSet();

                    // cache for future use
                    _highLevelRuleSets.Add(domainClass, highLevelRules);
                }
                ruleSets.Add(highLevelRules);
            }

            var rules  = new ValidationRuleSet(ruleSets);
            var result = rules.Test(obj, ruleFilter);

            if (result.Fail)
            {
                var message = string.Format(SR.ExceptionInvalidEntity, TerminologyTranslator.Translate(obj.GetClass()));
                throw new EntityValidationException(message, result.Reasons);
            }
        }
Esempio n. 3
0
		/// <summary>
		/// Validates the specified domain object, ignoring any rules that do not satisfy the filter.
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="level"></param>
		/// <param name="ruleFilter"></param>
		/// <exception cref="EntityValidationException">Validation failed.</exception>
		private void Validate(DomainObject obj, RuleLevel level, Predicate<ISpecification> ruleFilter)
		{
			var domainClass = obj.GetClass();

			var ruleSets = new List<ISpecification>();

			if (CheckLevel(level, RuleLevel.Low))
			{
				// first check for a cached rule-set
				ValidationRuleSet lowLevelRules;
				if (!_lowLevelRuleSets.TryGetValue(domainClass, out lowLevelRules))
				{
					// otherwise build it
					lowLevelRules = IsValidationEnabled(domainClass) ?
						ValidationRuleSetCache.GetLowLevelRules(domainClass)
						: new ValidationRuleSet();

					// cache for future use
					_lowLevelRuleSets.Add(domainClass, lowLevelRules);
				}
				ruleSets.Add(lowLevelRules);
			}

			if (CheckLevel(level, RuleLevel.High))
			{
				// first check for a cached rule-set
				ValidationRuleSet highLevelRules;
				if (!_highLevelRuleSets.TryGetValue(domainClass, out highLevelRules))
				{
					// otherwise build it
						highLevelRules = IsValidationEnabled(domainClass) ?
							ValidationRuleSetCache.GetHighLevelRules(domainClass)
						: new ValidationRuleSet();

						// cache for future use
						_highLevelRuleSets.Add(domainClass, highLevelRules);
					}
					ruleSets.Add(highLevelRules);
				}

			var rules = new ValidationRuleSet(ruleSets);
			var result = rules.Test(obj, ruleFilter);
			if (result.Fail)
			{
				var message = string.Format(SR.ExceptionInvalidEntity, TerminologyTranslator.Translate(obj.GetClass()));
				throw new EntityValidationException(message, result.Reasons);
			}
		}