コード例 #1
0
ファイル: ValidationContextHelper.cs プロジェクト: ziez/Catel
        /// <summary>
        /// Gets the changes between two different validation contexts.
        /// </summary>
        /// <param name="firstContext">The first context.</param>
        /// <param name="secondContext">The second context.</param>
        /// <returns>The list of changes.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="firstContext"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="secondContext"/> is <c>null</c>.</exception>
        public static List <ValidationContextChange> GetChanges(IValidationContext firstContext, IValidationContext secondContext)
        {
            Argument.IsNotNull("firstContext", firstContext);
            Argument.IsNotNull("secondContext", secondContext);

            var changes = new List <ValidationContextChange>();

            // Loop all fields, check removed items
            foreach (var fieldValidationResult in firstContext.GetFieldValidations())
            {
                var  secondContextFieldValidationResults = secondContext.GetFieldValidations(fieldValidationResult.PropertyName);
                bool stillContainsValidationResult       = (from result in secondContextFieldValidationResults
                                                            where result.ValidationResultType == fieldValidationResult.ValidationResultType &&
                                                            string.Compare(result.Message, fieldValidationResult.Message) == 0
                                                            select result).Any();

                if (!stillContainsValidationResult)
                {
                    changes.Add(new ValidationContextChange(fieldValidationResult, ValidationContextChangeType.Removed));
                }
            }

            // Loop all fields, check added items
            foreach (var fieldValidationResult in secondContext.GetFieldValidations())
            {
                var  firstContextFieldValidationResults = firstContext.GetFieldValidations(fieldValidationResult.PropertyName);
                bool existedInPreviousVersion           = (from result in firstContextFieldValidationResults
                                                           where result.ValidationResultType == fieldValidationResult.ValidationResultType &&
                                                           string.Compare(result.Message, fieldValidationResult.Message) == 0
                                                           select result).Any();

                if (!existedInPreviousVersion)
                {
                    changes.Add(new ValidationContextChange(fieldValidationResult, ValidationContextChangeType.Added));
                }
            }

            // Loop all business rules, check removed items
            foreach (var businessRuleValidation in firstContext.GetBusinessRuleValidations())
            {
                var  secondContextBusinessRuleValidationResults = secondContext.GetBusinessRuleValidations();
                bool stillContainsValidationResult = (from result in secondContextBusinessRuleValidationResults
                                                      where result.ValidationResultType == businessRuleValidation.ValidationResultType &&
                                                      string.Compare(result.Message, businessRuleValidation.Message) == 0
                                                      select result).Any();

                if (!stillContainsValidationResult)
                {
                    changes.Add(new ValidationContextChange(businessRuleValidation, ValidationContextChangeType.Removed));
                }
            }

            // Loop all business rules, check added items
            foreach (var businessRuleValidation in secondContext.GetBusinessRuleValidations())
            {
                var  firstContextBusinessRuleValidationResults = firstContext.GetBusinessRuleValidations();
                bool existedInPreviousVersion = (from result in firstContextBusinessRuleValidationResults
                                                 where result.ValidationResultType == businessRuleValidation.ValidationResultType &&
                                                 string.Compare(result.Message, businessRuleValidation.Message) == 0
                                                 select result).Any();

                if (!existedInPreviousVersion)
                {
                    changes.Add(new ValidationContextChange(businessRuleValidation, ValidationContextChangeType.Added));
                }
            }

            return(changes);
        }
コード例 #2
0
        /// <summary>
        /// Gets the changes between two different validation contexts.
        /// </summary>
        /// <param name="firstContext">The first context.</param>
        /// <param name="secondContext">The second context.</param>
        /// <returns>The list of changes.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="firstContext"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="secondContext"/> is <c>null</c>.</exception>
        public static List<ValidationContextChange> GetChanges(IValidationContext firstContext, IValidationContext secondContext)
        {
            Argument.IsNotNull("firstContext", firstContext);
            Argument.IsNotNull("secondContext", secondContext);

            var changes = new List<ValidationContextChange>();

            // Loop all fields, check removed items
            foreach (var fieldValidationResult in firstContext.GetFieldValidations())
            {
                var secondContextFieldValidationResults = secondContext.GetFieldValidations(fieldValidationResult.PropertyName);
                bool stillContainsValidationResult = (from result in secondContextFieldValidationResults
                                                      where result.ValidationResultType == fieldValidationResult.ValidationResultType &&
                                                            string.Compare(result.Message, fieldValidationResult.Message) == 0
                                                      select result).Any();

                if (!stillContainsValidationResult)
                {
                    changes.Add(new ValidationContextChange(fieldValidationResult, ValidationContextChangeType.Removed));
                }
            }

            // Loop all fields, check added items
            foreach (var fieldValidationResult in secondContext.GetFieldValidations())
            {
                var firstContextFieldValidationResults = firstContext.GetFieldValidations(fieldValidationResult.PropertyName);
                bool existedInPreviousVersion = (from result in firstContextFieldValidationResults
                                                 where result.ValidationResultType == fieldValidationResult.ValidationResultType &&
                                                       string.Compare(result.Message, fieldValidationResult.Message) == 0
                                                 select result).Any();

                if (!existedInPreviousVersion)
                {
                    changes.Add(new ValidationContextChange(fieldValidationResult, ValidationContextChangeType.Added));
                }
            }

            // Loop all business rules, check removed items
            foreach (var businessRuleValidation in firstContext.GetBusinessRuleValidations())
            {
                var secondContextBusinessRuleValidationResults = secondContext.GetBusinessRuleValidations();
                bool stillContainsValidationResult = (from result in secondContextBusinessRuleValidationResults
                                                      where result.ValidationResultType == businessRuleValidation.ValidationResultType &&
                                                            string.Compare(result.Message, businessRuleValidation.Message) == 0
                                                      select result).Any();

                if (!stillContainsValidationResult)
                {
                    changes.Add(new ValidationContextChange(businessRuleValidation, ValidationContextChangeType.Removed));
                }
            }

            // Loop all business rules, check added items
            foreach (var businessRuleValidation in secondContext.GetBusinessRuleValidations())
            {
                var firstContextBusinessRuleValidationResults = firstContext.GetBusinessRuleValidations();
                bool existedInPreviousVersion = (from result in firstContextBusinessRuleValidationResults
                                                 where result.ValidationResultType == businessRuleValidation.ValidationResultType &&
                                                       string.Compare(result.Message, businessRuleValidation.Message) == 0
                                                 select result).Any();

                if (!existedInPreviousVersion)
                {
                    changes.Add(new ValidationContextChange(businessRuleValidation, ValidationContextChangeType.Added));
                }
            }

            return changes;
        }