Exemple #1
0
 /// <summary>
 /// 更新指定策略的验证结果
 /// </summary>
 /// <param name="collection">验证结果集合</param>
 private void UpdateResultFlaggedByStrategy(ValidationResultCollection collection)
 {
     if (collection == null)
     {
         return;
     }
     foreach (var set in collection._resultsFlaggedByStrategy)
     {
         UpdateResultFlaggedByStrategy(set.Key, set.Value);
     }
 }
Exemple #2
0
 /// <summary>
 /// 初始化一个<see cref="ValidationResultCollection"/>类型的实例
 /// </summary>
 /// <param name="collection">验证结果集合</param>
 public ValidationResultCollection(ValidationResultCollection collection) : this()
 {
     if (collection == null)
     {
         throw new ArgumentNullException(nameof(collection));
     }
     ErrorCode = collection.ErrorCode;
     Flag      = collection.Flag;
     _results  = collection._results;
     UpdateResultFlaggedByStrategy(collection);
 }
Exemple #3
0
        /// <summary>
        /// 转换为异常
        /// </summary>
        /// <param name="resultCollection">验证结果集合</param>
        /// <param name="appendAction">追加操作</param>
        public static ValidationException ToException(this ValidationResultCollection resultCollection,
                                                      Action <ValidationException, ValidationResultCollection> appendAction = null)
        {
            if (resultCollection == null)
            {
                throw new ArgumentNullException(nameof(resultCollection));
            }
            var exception = CreateValidationException(resultCollection);

            appendAction?.Invoke(exception, resultCollection);
            return(exception);
        }
Exemple #4
0
        /// <summary>
        /// 转换异常
        /// </summary>
        /// <typeparam name="TException">异常类型</typeparam>
        /// <param name="resultCollection">验证结果集合</param>
        /// <param name="appendAction">追加操作</param>
        public static TException ToException <TException>(this ValidationResultCollection resultCollection,
                                                          Action <TException, ValidationResultCollection> appendAction = null) where TException : BingException, new()
        {
            if (resultCollection == null)
            {
                throw new ArgumentNullException(nameof(resultCollection));
            }
            if (typeof(TException) == typeof(ValidationException))
            {
                return(ToException(resultCollection, (e, c) => appendAction?.Invoke(e as TException, c)) as TException);
            }
            var exception = CreateBasicException <TException>(resultCollection);

            appendAction?.Invoke(exception, resultCollection);
            return(exception);
        }
Exemple #5
0
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="target">验证目标</param>
        public static ValidationResultCollection Validate(object target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            var result            = new ValidationResultCollection();
            var validationResults = new List <ValidationResult>();
            var context           = new ValidationContext(target, null, null);
            var isValid           = Validator.TryValidateObject(target, context, validationResults, true);

            if (!isValid)
            {
                result.AddRange(validationResults);
            }
            return(result);
        }
Exemple #6
0
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="appendAction">追加操作</param>
        public void Validate(Action <ValidationResultCollection> appendAction = null)
        {
            var result = DataAnnotationValidation.Validate(Instance);

            ResultCollection = new ValidationResultCollection(result);
            appendAction?.Invoke(ResultCollection);
            var tempList = ValidateStrategyList.Select(strategy => strategy.Validate(Instance)).ToList();

            if (tempList.Any())
            {
                ResultCollection.AddRange(tempList);
            }
            if (ResultCollection.IsValid)
            {
                return;
            }
            if (Handle == null)
            {
                Handle = op => op.HandleAll(BingConfig.Current.ValidationHandler);// 如果没有处理器操作,则使用默认操作
            }
            Handle.Invoke(ResultCollection.Handle());
        }
Exemple #7
0
 /// <summary>
 /// 引发异常
 /// </summary>
 /// <param name="resultCollection">验证结果集合</param>
 /// <param name="appendAction">追加操作</param>
 public static void RaiseException(this ValidationResultCollection resultCollection,
                                   Action <ValidationException, ValidationResultCollection> appendAction = null) =>
 throw resultCollection.ToException(appendAction);
Exemple #8
0
 /// <summary>
 /// 引发异常
 /// </summary>
 /// <typeparam name="TException">异常类型</typeparam>
 /// <param name="resultCollection">验证结果集合</param>
 /// <param name="appendAction">追加操作</param>
 public static void RaiseException <TException>(this ValidationResultCollection resultCollection,
                                                Action <TException, ValidationResultCollection> appendAction = null) where TException : BingException, new() =>
 throw resultCollection.ToException(appendAction);
Exemple #9
0
 /// <summary>
 /// 处理
 /// </summary>
 /// <param name="collection">验证结果集合</param>
 public static ValidationHandleOperation Handle(this ValidationResultCollection collection) => new ValidationHandleOperation(collection);
 /// <summary>
 /// 初始化一个<see cref="ValidationHandleOperation"/>
 /// </summary>
 /// <param name="collection">验证结果集合</param>
 public ValidationHandleOperation(ValidationResultCollection collection) => _collection = collection ?? throw new ArgumentNullException(nameof(collection));