public Validator AddRule(Type type, IValidator validator, ErrorMessage msg)
        {
            ValidationUnitCollection coll = GetRules(type);

            coll.Add(ValidationUnit.CreateValidationUnit(msg, validator));
            return(this);
        }
Esempio n. 2
0
 /// <summary>
 /// This is the base class to add validation programmatically, due to dynamic discovery of
 /// the attributes, when a type is inserted through this interface no dynamic scan for attribute
 /// is done.
 /// </summary>
 /// <param name="objType"></param>
 /// <param name="validationUnit"></param>
 public void AddValidationRule(
     Type objType,
     ValidationUnit validationUnit)
 {
     if (!mValidationRules.ContainsKey(objType))
     {
         mValidationRules.Add(objType, new ValidationUnitCollection());
     }
     mValidationRules[objType].Add(validationUnit);
 }
Esempio n. 3
0
        /// <summary>
        /// Starting from current type this routine scan all field and property of the type to
        /// find if some property of type contains a validatable object, when found that object is rescanned
        /// </summary>
        /// <param name="ruleMap"></param>
        /// <param name="typeToCheck"></param>
        /// <param name="listOfLoadedTypes">Since some objects could be already loaded, we check
        /// all properties and fields of the new types of object that are inserted.</param>
        /// <param name="listOfScannedTypes">This is an inner list that check type already scanned,
        /// consider what happended if object A has three property of type B, we do not want to
        /// scan Three times objectB and setting an excessive number of validator.</param>
        private void RecursiveSetObjectValidator(
            Type typeToCheck,
            Dictionary <Type, ValidationUnitCollection> ruleMap,
            IList <Type> listOfLoadedTypes,
            IList <Type> listOfScannedTypes)
        {
            //If this type was not in the listofloadedtype there is no need to check, probably its set or rules is already loaded
            if (!listOfLoadedTypes.Contains(typeToCheck))
            {
                return;
            }

            listOfScannedTypes.Add(typeToCheck);

            System.Diagnostics.Debug.Assert(ruleMap.ContainsKey(typeToCheck), "We are scanning an object that where not scanned for rules");
            //now we should check for each property and field, we need to find
            foreach (FieldInfo fi in typeToCheck.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldValidateWithObjectValidator(ruleMap, fi.FieldType))
                {
                    IValueExtractor extractor = new FieldInfoValueExtractor(fi);
                    ruleMap[typeToCheck].Add(
                        ValidationUnit.CreateObjectValidationUnit(
                            extractor, fi.Name, ruleMap));
                }
                Type type = GetTypeFromInnerMember(fi.FieldType);
                if (!listOfScannedTypes.Contains(type))
                {
                    RecursiveSetObjectValidator(type, ruleMap, listOfLoadedTypes, listOfScannedTypes);
                }
            }

            foreach (PropertyInfo pi in typeToCheck.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (ShouldValidateWithObjectValidator(ruleMap, pi.PropertyType))
                {
                    IValueExtractor extractor = new PropertyInfoValueExtractor(pi);
                    ruleMap[typeToCheck].Add(
                        ValidationUnit.CreateObjectValidationUnit(
                            extractor, pi.Name, ruleMap));
                }
                Type type = GetTypeFromInnerMember(pi.PropertyType);
                if (!listOfScannedTypes.Contains(type))
                {
                    RecursiveSetObjectValidator(type, ruleMap, listOfLoadedTypes, listOfScannedTypes);
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Given an array of object of validation attributes create all
 /// requested ValidationUnits.
 /// </summary>
 /// <param name="vc"></param>
 ///<param name="valueExtractor">The value extractor proposed by the caller
 /// the real value extractor can be overriden by the attribute.</param>
 /// <param name="validationfields"></param>
 private static void BuildValidationUnitFromAttributeList(
     ValidationUnitCollection vc,
     IValueExtractor valueExtractor,
     object[] validationfields)
 {
     foreach (BaseValidationAttribute va in validationfields)
     {
         if (va.IsValueExtractorOverriden)
         {
             valueExtractor = va.CreateValueExtractor();
         }
         vc.Add(ValidationUnit.CreateValidationUnit(
                    va.CreateErrorMessage(),
                    va.CreateValidator(valueExtractor)));
     }
 }
 public void Add(ValidationUnit validationUnit)
 {
     mList.Add(validationUnit);
 }
Esempio n. 6
0
 public void Add(ValidationUnit validationUnit)
 {
     mList.Add(validationUnit);
 }