Esempio n. 1
0
        /// <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(result.Errors[0].ErrorMessage, ResultCodes.InvalidParameter));
            }
            else
            {
                return(Result.ReSuccess());
            }
        }
Esempio n. 2
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(), result.Errors.ToJsonString()), null, (object state, Exception error) =>
                {
                    return(state.ToString());
                });
            }
            return(!result.HasError);
        }