/// <summary>
        /// 验证是否成功
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="logger">日志记录</param>
        /// <param name="logLevel">日志记录级别</param>
        /// <returns></returns>
        public static bool IsValid <T>(T entity, ILogger logger, LogLevel logLevel = LogLevel.Information)
            where T : class
        {
            EntityValidationResult result = ValidateEntity <T>(entity);

            return(!result.HasError);
        }
        /// <summary>
        /// 验证是否成功
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public static Result ValidResult <T>(T entity)
            where T : class
        {
            EntityValidationResult result = ValidateEntity <T>(entity);

            if (result.HasError)
            {
                return(Result.ReFailure(BaseResultCodes.BadRequest.ToFormat(result.Errors[0].ErrorMessage)));
            }
            else
            {
                return(Result.ReSuccess());
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 验证是否成功
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="logger">日志记录</param>
        /// <param name="logLevel">日志记录级别</param>
        /// <returns></returns>
        public static bool IsValid <T>(T entity, ILogger logger, LogLevel logLevel = LogLevel.Information)
            where T : class
        {
            EntityValidationResult result = ValidateEntity <T>(entity);

            if (result.HasError && logger != null)
            {
                logger.Log(logLevel, 0, new FormattedLogValues("{EntityType} Valid Fail Message:{error}", entity.GetType(), JsonConvert.SerializeObject(result.Errors)), null, (object state, Exception error) =>
                {
                    return(state.ToString());
                });
            }
            return(!result.HasError);
        }