Пример #1
0
        /// <summary>
        /// Gets the validation context for a complete object graph by also checking the properties and recursive
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The validation context for the whole object graph.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public static IValidationContext GetValidationContextForObjectGraph(this IValidatableModel model)
        {
            Argument.IsNotNull("model", model);

            var validationContext = new ValidationContext();

            validationContext.AddModelValidation(model, new List <IValidatableModel>());

            return(validationContext);
        }
        private static void AddModelValidation(this ValidationContext validationContext, IModel model, List <IModel> handledModels)
        {
            Argument.IsNotNull("validationContext", validationContext);


            if (handledModels.Any(x => ReferenceEquals(x, model)))
            {
                return;
            }

            handledModels.Add(model);

            validationContext.SynchronizeWithContext(model.ValidationContext, true);

            var propertyDataManager = PropertyDataManager.Default;
            var catelTypeInfo       = propertyDataManager.GetCatelTypeInfo(model.GetType());

            foreach (var property in catelTypeInfo.GetCatelProperties())
            {
                var propertyValue = model.GetValue(property.Key);
                var enumerable    = propertyValue as IEnumerable;
                if (enumerable != null && !(propertyValue is string))
                {
                    foreach (var item in enumerable)
                    {
                        var modelItem = item as IModel;
                        if (modelItem != null)
                        {
                            validationContext.AddModelValidation(modelItem, handledModels);
                        }
                    }
                }

                var propertyModel = propertyValue as IModel;
                if (propertyModel != null)
                {
                    validationContext.AddModelValidation(propertyModel, handledModels);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Gets the validation context for a complete object graph by also checking the properties and recursive 
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>The validation context for the whole object graph.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public static IValidationContext GetValidationContextForObjectGraph(this IModel model)
        {
            Argument.IsNotNull("model", model);

            var validationContext = new ValidationContext();

            validationContext.AddModelValidation(model, new List<IModel>());

            return validationContext;
        }