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
 private TestResult TestCollection(IEnumerable collection, Predicate <ISpecification> filter)
 {
     foreach (var item in collection)
     {
         var result = _innerRules.Test(item, filter);
         // if any item fails, don't bother testing the rest of the items
         if (result.Fail)
         {
             var message = string.Format(SR.RuleEmbeddeValueCollection, TerminologyTranslator.Translate(this.Property));
             return(new TestResult(false, new TestResultReason(message, result.Reasons)));
         }
     }
     return(new TestResult(true));
 }
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);
            }
        }
Esempio n. 4
0
 protected virtual string GetMessage(DomainObject obj)
 {
     return(string.Format(SR.RuleUniqueKey, TerminologyTranslator.Translate(obj.GetClass(), _logicalKeyName),
                          TerminologyTranslator.Translate(_entityClass)));
 }
 private string GetMessage()
 {
     return(string.Format(SR.RuleRequired, TerminologyTranslator.Translate(this.Property)));
 }
Esempio n. 6
0
 private string GetMessage()
 {
     return(string.Format(SR.RuleLength,
                          TerminologyTranslator.Translate(this.Property), _min, _max));
 }