예제 #1
0
        /// <summary>
        /// Generate code to perform required validation on a type
        /// </summary>
        /// <param name="type">The type to validate</param>
        /// <param name="scope">A scope provider for generating variable names as necessary</param>
        /// <param name="valueReference">A reference to the value being validated</param>
        /// <param name="constraints">Constraints</param>
        /// <returns>The code to validate the reference of the given type</returns>
        public static string ValidateType(this IType type, IScopeProvider scope, string valueReference,
                                          Dictionary <Constraint, string> constraints)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

            CompositeType  model      = type as CompositeType;
            SequenceType   sequence   = type as SequenceType;
            DictionaryType dictionary = type as DictionaryType;

            var sb = new IndentedStringBuilder();

            if (model != null && model.ShouldValidateChain())
            {
                sb.AppendLine("{0}.Validate();", valueReference);
            }

            if (constraints != null && constraints.Any())
            {
                AppendConstraintValidations(valueReference, constraints, sb);
            }

            if (sequence != null && sequence.ShouldValidateChain())
            {
                var elementVar      = scope.GetVariableName("element");
                var innerValidation = sequence.ElementType.ValidateType(scope, elementVar, null);
                if (!string.IsNullOrEmpty(innerValidation))
                {
                    sb.AppendLine("foreach (var {0} in {1})", elementVar, valueReference)
                    .AppendLine("{").Indent()
                    .AppendLine(innerValidation).Outdent()
                    .AppendLine("}");
                }
            }
            else if (dictionary != null && dictionary.ShouldValidateChain())
            {
                var valueVar        = scope.GetVariableName("valueElement");
                var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar, null);
                if (!string.IsNullOrEmpty(innerValidation))
                {
                    sb.AppendLine("foreach (var {0} in {1}.Values)", valueVar, valueReference)
                    .AppendLine("{").Indent()
                    .AppendLine(innerValidation).Outdent()
                    .AppendLine("}").Outdent();
                }
            }

            if (sb.ToString().Trim().Length > 0)
            {
                return(CheckNull(valueReference, sb.ToString()));
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Generate code to perform required validation on a type
        /// </summary>
        /// <param name="type">The type to validate</param>
        /// <param name="scope">A scope provider for generating variable names as necessary</param>
        /// <param name="valueReference">A reference to the value being validated</param>
        /// <returns>The code to validate the reference of the given type</returns>
        public static string ValidateType(this IType type, IScopeProvider scope, string valueReference)
        {
            if (scope == null)
            {
                throw new ArgumentNullException("scope");
            }

            CompositeType  model      = type as CompositeType;
            SequenceType   sequence   = type as SequenceType;
            DictionaryType dictionary = type as DictionaryType;

            if (model != null && model.ShouldValidateChain())
            {
                return(CheckNull(valueReference, string.Format(CultureInfo.InvariantCulture,
                                                               "{0}.Validate();", valueReference)));
            }
            if (sequence != null && sequence.ShouldValidateChain())
            {
                var elementVar      = scope.GetVariableName("element");
                var innerValidation = sequence.ElementType.ValidateType(scope, elementVar);
                if (!string.IsNullOrEmpty(innerValidation))
                {
                    var sb = new IndentedStringBuilder();
                    sb.AppendLine("foreach (var {0} in {1})", elementVar, valueReference)
                    .AppendLine("{").Indent()
                    .AppendLine(innerValidation).Outdent()
                    .AppendLine("}");
                    return(CheckNull(valueReference, sb.ToString()));
                }
            }
            else if (dictionary != null && dictionary.ShouldValidateChain())
            {
                var valueVar        = scope.GetVariableName("valueElement");
                var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar);
                if (!string.IsNullOrEmpty(innerValidation))
                {
                    var sb = new IndentedStringBuilder();
                    sb.AppendLine("if ({0} != null)", valueReference)
                    .AppendLine("{").Indent()
                    .AppendLine("foreach (var {0} in {1}.Values)", valueVar, valueReference)
                    .AppendLine("{").Indent()
                    .AppendLine(innerValidation).Outdent()
                    .AppendLine("}").Outdent()
                    .AppendLine("}");

                    return(CheckNull(valueReference, sb.ToString()));
                }
            }

            return(null);
        }