public static IValidationResult[] GetValidationResults(IValidatableEntity validatableEntity, SharedUtility.ObjectVisitationHelper visitationHelper)
        {
            var type         = validatableEntity.GetType();
            var nonValidated = SharedUtility.ApplicationContext.DomainGraphCache.GetPropertiesAttributes <NonValidated>(type);
            var brokenRules  = new List <IValidationResult>(validatableEntity.RuleValidationManager.Results);
            var children     = new List <PropertyInfo>(SharedUtility.ApplicationContext.DomainGraphCache.GetPropertiesOfType <IValidatableEntity>(type).Where(x => !nonValidated.ContainsKey(x)));

            children.AddRange(new List <PropertyInfo>(SharedUtility.ApplicationContext.DomainGraphCache.GetPropertiesOfType <IEnumerable <IValidatableEntity> >(type).Where(x => !nonValidated.ContainsKey(x))));

            visitationHelper.TryVisit(validatableEntity);

            foreach (var child in children)
            {
                var childValue = child.GetValue(validatableEntity, null);

                if (childValue is IEnumerable <IValidatableEntity> )
                {
                    var values = child.GetValue(validatableEntity, null) as IEnumerable <IValidatableEntity>;

                    foreach (var value in values)
                    {
                        if (value != null)
                        {
                            if (!visitationHelper.TryVisit(value))
                            {
                                continue;
                            }

                            brokenRules.AddRange(GetValidationResults(value, visitationHelper));
                        }
                    }
                }
                else
                {
                    var value = childValue as IValidatableEntity;

                    if (value != null)
                    {
                        if (!visitationHelper.TryVisit(value))
                        {
                            continue;
                        }

                        brokenRules.AddRange(GetValidationResults(value, visitationHelper));
                    }
                }
            }

            return(brokenRules.ToArray());
        }
示例#2
0
        private void WalkInternal(T entity, Action <T> action, ObjectVisitationHelper visitationHelper)
        {
            if (!visitationHelper.TryVisit(entity))
            {
                return;
            }

            action(entity);

            foreach (var propertyInfo in entity.GetType().GetProperties())
            {
                if (propertyInfo.GetIndexParameters().Length > 0)
                {
                    continue;
                }

                var isEntitySingular   = typeof(T).IsAssignableFrom(propertyInfo.PropertyType);
                var isEntityCollection = typeof(IEnumerable <T>).IsAssignableFrom(propertyInfo.PropertyType);

                if (isEntitySingular)
                {
                    var propertyEntity = propertyInfo.GetValue(entity, null) as T;

                    if (propertyEntity != null)
                    {
                        WalkInternal(propertyEntity, action, visitationHelper);
                    }
                }
                else if (isEntityCollection)
                {
                    var propertyEntities = propertyInfo.GetValue(entity, null) as IEnumerable <T>;

                    if (propertyEntities != null)
                    {
                        propertyEntities.Each((e) => WalkInternal(e, action, visitationHelper));
                    }
                }
            }
        }