Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="validatedElement"></param>
        /// <param name="validatorBuilderCreator"></param>
        /// <returns></returns>
        protected Validator CreateValidatorForValidatedElement(
            IValidatedElement validatedElement,
            CompositeValidatorBuilderCreator validatorBuilderCreator)
        {
            IEnumerator <IValidatorDescriptor> validatorDescriptorsEnumerator =
                validatedElement.GetValidatorDescriptors().GetEnumerator();

            if (!validatorDescriptorsEnumerator.MoveNext())
            {
                return(null);
            }

            CompositeValidatorBuilder validatorBuilder = validatorBuilderCreator(validatedElement);

            do
            {
                Validator validator =
                    validatorDescriptorsEnumerator.Current
                    .CreateValidator(
                        validatedElement.TargetType,
                        validatedElement.MemberInfo.ReflectedType,
                        this.memberAccessValidatorFactory.MemberValueAccessBuilder,
                        this.validatorFactory);
                validatorBuilder.AddValueValidator(validator);
            }while (validatorDescriptorsEnumerator.MoveNext());

            return(validatorBuilder.GetValidator());
        }
 /// <summary>
 /// Create a <see cref="Validator"/> instance based on the validation
 /// attributes on the given parameter.
 /// </summary>
 /// <remarks>This factory method ignores configuration and rulesets. For parameters,
 /// only attribute based configuration is supported at this time.</remarks>
 /// <param name="paramInfo">The <see cref="ParameterInfo"/> for the parameter to construct a validator for.</param>
 /// <returns>The <see cref="Validator"/></returns>
 public static Validator CreateValidator(ParameterInfo paramInfo)
 {
     MetadataValidatedParameterElement parameterElement = new MetadataValidatedParameterElement();
     parameterElement.UpdateFlyweight(paramInfo);
     CompositeValidatorBuilder compositeBuilder = new CompositeValidatorBuilder(parameterElement);
     foreach (IValidatorDescriptor descriptor in parameterElement.GetValidatorDescriptors())
     {
         compositeBuilder.AddValueValidator(
             descriptor.CreateValidator(
                 paramInfo.ParameterType,
                 null,
                 null,
                 ValidationFactory.DefaultCompositeValidatorFactory));
     }
     return compositeBuilder.GetValidator();
 }
Exemplo n.º 3
0
        /// <summary>
        /// Create a <see cref="Validator"/> instance based on the validation
        /// attributes on the given parameter.
        /// </summary>
        /// <remarks>This factory method ignores configuration and rulesets. For parameters,
        /// only attribute based configuration is supported at this time.</remarks>
        /// <param name="paramInfo">The <see cref="ParameterInfo"/> for the parameter to construct a validator for.</param>
        /// <returns>The <see cref="Validator"/></returns>
        public static Validator CreateValidator(ParameterInfo paramInfo)
        {
            MetadataValidatedParameterElement parameterElement = new MetadataValidatedParameterElement();

            parameterElement.UpdateFlyweight(paramInfo);
            CompositeValidatorBuilder compositeBuilder = new CompositeValidatorBuilder(parameterElement);

            foreach (IValidatorDescriptor descriptor in parameterElement.GetValidatorDescriptors())
            {
                compositeBuilder.AddValueValidator(
                    descriptor.CreateValidator(
                        paramInfo.ParameterType,
                        null,
                        null,
                        ValidationFactory.DefaultCompositeValidatorFactory));
            }
            return(compositeBuilder.GetValidator());
        }