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

            coll.Add(ValidationUnit.CreateValidationUnit(msg, validator));
            return(this);
        }
        /// <summary>
        /// Recursive scan type to find all validator defined for each object of the graph.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="loadList"></param>
        /// <param name="typeToCheck"></param>
        private void RecursivePopulate(
            Type typeToCheck,
            Dictionary <Type, ValidationUnitCollection> list,
            IList <Type> loadList)
        {
            //first check if the type was already loaded or is in ignorelist.
            if (list.ContainsKey(typeToCheck))
            {
                return;
            }

            //Type is not contained so it is necessary to check, first prepare parameter collections
            List <Type> relatedTypes    = new List <Type>();
            ValidationUnitCollection vc = new ValidationUnitCollection();

            list.Add(typeToCheck, vc);
            loadList.Add(typeToCheck);

            //Then scan for property or fields.
            ScanForTypeAttributes(typeToCheck, vc);
            ScanForField(typeToCheck, vc, relatedTypes);
            ScanForProperty(typeToCheck, vc, relatedTypes);
            relatedTypes.ForEach(delegate(Type ty)
            {
                RecursivePopulate(ty, list, loadList);
            });
        }
        /// <summary>
        /// This function scan type of object to find all validators related to object themselves.
        /// </summary>
        /// <param name="ty"></param>
        public ValidationUnitCollection Scan()
        {
            ValidationUnitCollection vc = new ValidationUnitCollection();

            PopulateValidationUnitcollection(vc);
            return(vc);
        }
 /// <summary>
 /// This function populate the collection
 /// </summary>
 /// <param name="vc"></param>
 /// <returns>This function return the inner list of all type that are
 /// used in field And/Or property</returns>
 private void PopulateValidationUnitcollection(
     ValidationUnitCollection vc)
 {
     ScanForTypeAttributes(mType, vc);
     ScanForField(mType, vc, null);
     ScanForProperty(mType, vc, null);
 }
 /// <summary>
 /// Scan the type and check if there are some attributes defined on the whole type.
 /// </summary>
 /// <param name="typeToCheck"></param>
 /// <param name="vc"></param>
 private void ScanForTypeAttributes(
     Type typeToCheck,
     ValidationUnitCollection vc)
 {
     object[] validationfields = typeToCheck.GetCustomAttributes(typeof(BaseValidationAttribute), false);
     BuildValidationUnitFromAttributeList(vc, new ObjectValueExtractor(), validationfields);
 }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="objToValidate">The object that is being validated.</param>
        /// <param name="validationFlags">Options used to decide how validation is performed</param>
        /// <returns></returns>
        public ValidationResult ValidateObject(
            object objToValidate,
            ValidationFlags validationFlags)
        {
            ValidationUnitCollection rules  = GetRules(objToValidate);
            ValidationResult         result = new ValidationResult();

            return(rules.ValidateObject(result, objToValidate, validationFlags));
        }
 /// <summary>
 /// Scan all fields of the type and populate both the collection of rules and other types encountered.
 /// </summary>
 /// <param name="vc"></param>
 /// <param name="fieldTypes"></param>
 private void ScanForField(
     Type typeToCheck,
     ValidationUnitCollection vc,
     List <Type> fieldTypes)
 {
     FieldInfo[] afi = typeToCheck.GetFields(BindingFlags.Instance | BindingFlags.Public);
     foreach (FieldInfo fi in afi)
     {
         object[] validationfields = fi.GetCustomAttributes(typeof(BaseValidationAttribute), false);
         BuildValidationUnitFromAttributeList(vc, new FieldInfoValueExtractor(fi), validationfields);
         PopulateDescendantList(fi.FieldType, fieldTypes);
     }
 }
 private void ScanForProperty(
     Type typeToCheck,
     ValidationUnitCollection vc,
     List <Type> propertyTypes)
 {
     PropertyInfo[] api = typeToCheck.GetProperties(BindingFlags.Instance | BindingFlags.Public);
     foreach (PropertyInfo pi in api)
     {
         object[] validationfields = pi.GetCustomAttributes(typeof(BaseValidationAttribute), false);
         BuildValidationUnitFromAttributeList(vc, new PropertyInfoValueExtractor(pi), validationfields);
         PopulateDescendantList(pi.PropertyType, propertyTypes);
     }
 }
 /// <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)));
     }
 }
Beispiel #10
0
        /// <summary>
        /// This function scan type of object to find all validators related to object themselves.
        /// This new version check actually for each type in object graph.
        /// </summary>
        /// <param name="ty"></param>
        public void ScanTypeForAttribute(Type ty, Boolean recursive)
        {
            //Create and add the collection to this type
            TypeScanner ts = new TypeScanner(ty);

            if (recursive)
            {
                ts.RecursiveScan(mValidationRules);
            }
            else
            {
                ValidationUnitCollection vc = ts.Scan();
                mValidationRules.Add(ty, vc);
            }
        }
 /// <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)));
     }
 }
 /// <summary>
 /// This function populate the collection
 /// </summary>
 /// <param name="vc"></param>
 /// <returns>This function return the inner list of all type that are
 /// used in field And/Or property</returns>
 private void PopulateValidationUnitcollection(
     ValidationUnitCollection vc)
 {
     ScanForTypeAttributes(mType, vc);
     ScanForField(mType, vc, null);
     ScanForProperty(mType, vc, null);
 }
        /// <summary>
        /// Recursive scan type to find all validator defined for each object of the graph.
        /// </summary>
        /// <param name="list"></param>
        /// <param name="loadList"></param>
        /// <param name="typeToCheck"></param>
        private void RecursivePopulate(
            Type typeToCheck,
            Dictionary<Type, ValidationUnitCollection> list,
            IList<Type> loadList)
        {
            //first check if the type was already loaded or is in ignorelist.
            if (list.ContainsKey(typeToCheck)) return;

            //Type is not contained so it is necessary to check, first prepare parameter collections
            List<Type> relatedTypes = new List<Type>();
            ValidationUnitCollection vc = new ValidationUnitCollection();
            list.Add(typeToCheck, vc);
            loadList.Add(typeToCheck);

            //Then scan for property or fields.
            ScanForTypeAttributes(typeToCheck, vc);
            ScanForField(typeToCheck, vc, relatedTypes);
            ScanForProperty(typeToCheck, vc, relatedTypes);
            relatedTypes.ForEach(delegate(Type ty)
            {
                RecursivePopulate(ty, list, loadList);
            });
        }
 /// <summary>
 /// Scan all fields of the type and populate both the collection of rules and other types encountered.
 /// </summary>
 /// <param name="vc"></param>
 /// <param name="fieldTypes"></param>
 private void ScanForField(
     Type typeToCheck,
     ValidationUnitCollection vc,
     List<Type> fieldTypes)
 {
     FieldInfo[] afi = typeToCheck.GetFields(BindingFlags.Instance | BindingFlags.Public);
     foreach (FieldInfo fi in afi)
     {
         object[] validationfields = fi.GetCustomAttributes(typeof(BaseValidationAttribute), false);
         BuildValidationUnitFromAttributeList(vc, new FieldInfoValueExtractor(fi), validationfields);
         PopulateDescendantList(fi.FieldType, fieldTypes);
     }
 }
 private void ScanForProperty(
     Type typeToCheck,
     ValidationUnitCollection vc,
     List<Type> propertyTypes)
 {
     PropertyInfo[] api = typeToCheck.GetProperties(BindingFlags.Instance | BindingFlags.Public);
     foreach (PropertyInfo pi in api)
     {
         object[] validationfields = pi.GetCustomAttributes(typeof(BaseValidationAttribute), false);
         BuildValidationUnitFromAttributeList(vc, new PropertyInfoValueExtractor(pi), validationfields);
         PopulateDescendantList(pi.PropertyType, propertyTypes);
     }
 }
 /// <summary>
 /// Scan the type and check if there are some attributes defined on the whole type.
 /// </summary>
 /// <param name="typeToCheck"></param>
 /// <param name="vc"></param>
 private void ScanForTypeAttributes(
     Type typeToCheck,
     ValidationUnitCollection vc)
 {
     object[] validationfields = typeToCheck.GetCustomAttributes(typeof(BaseValidationAttribute), false);
     BuildValidationUnitFromAttributeList(vc, new ObjectValueExtractor(), validationfields);
 }
 /// <summary>
 /// This function scan type of object to find all validators related to object themselves.
 /// </summary>
 /// <param name="ty"></param>
 public ValidationUnitCollection Scan()
 {
     ValidationUnitCollection vc = new ValidationUnitCollection();
     PopulateValidationUnitcollection(vc);
     return vc;
 }