コード例 #1
0
        private void Validate(ValidatorResult result, T entity, ValidationTypes type)
        {
            switch (type)
            {
            case ValidationTypes.Default:
                DefaultValidations(result, entity);
                break;

            case ValidationTypes.Insert:
                InsertValidations(result, entity);
                break;

            case ValidationTypes.Update:
                UpdateValidations(result, entity);
                break;

            case ValidationTypes.Delete:
                DeleteValidations(result, entity);
                break;

            default:
                DefaultValidations(result, entity);
                break;
            }
        }
コード例 #2
0
        public static void ValidateAnnotations(this ValidatorResult result, object entity)
        {
            if (entity == null)
            {
                return;
            }

            var ctx = new ValidationContext(entity);

            var type  = entity.GetType();
            var props = type.GetProperties();

            foreach (var prop in props)
            {
                var value = prop.GetValue(entity, null);
                ctx.MemberName = prop.Name;
                var validationList = new List <ValidationResult>();
                Validator.TryValidateProperty(value, ctx, validationList);

                foreach (var validation in validationList)
                {
                    result.AddError(validation.ErrorMessage);
                }
            }
        }
コード例 #3
0
        public ValidatorResult Validate(T entity, ValidationTypes type = ValidationTypes.Default)
        {
            var result = new ValidatorResult();

            Validate(result, entity, type);

            return(result);
        }
コード例 #4
0
        protected override void DefaultValidations(ValidatorResult result, User entity)
        {
            base.DefaultValidations(result, entity);

            if (Service.GetAll(x => x.Login.ToLower() == entity.Login.ToLower() && x.Id != entity.Id).Any())
            {
                result.AddError($"There is already a user with this Login: {entity.Login}");
            }
        }
コード例 #5
0
        protected virtual void DefaultValidations(ValidatorResult result, T entity)
        {
            AnnotationsValidations(result, entity);

            if (entity == null)
            {
                result.AddError("Entity can not be null!");
            }
        }
コード例 #6
0
        public ValidatorResult Validate(T[] entities, ValidationTypes type = ValidationTypes.Default)
        {
            var result = new ValidatorResult();

            foreach (var entity in entities)
            {
                Validate(result, entity, type);
            }

            return(result);
        }
コード例 #7
0
 protected virtual void DeleteValidations(ValidatorResult result, T entity)
 {
 }
コード例 #8
0
 protected virtual void UpdateValidations(ValidatorResult result, T entity)
 {
     DefaultValidations(result, entity);
 }
コード例 #9
0
 protected virtual void AnnotationsValidations(ValidatorResult result, T entity)
 {
     result.ValidateAnnotations(entity);
 }