using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using Znyc.Cloudcar.Admin.Commons.Dtos; using Znyc.Cloudcar.Admin.Commons.Properties; namespace Znyc.Cloudcar.Admin.Commons.Data { /// /// 参数合法性检查类 /// [DebuggerStepThrough] public static class Check { /// /// 验证指定值的断言是否为真,如果不为真,抛出指定消息的指定类型 /// 异常 /// /// 异常类型 /// 要验证的断言。 /// 异常消息。 private static void Require(bool assertion, string message) where TException : Exception { if (assertion) { return; } if (string.IsNullOrEmpty(message)) { throw new ArgumentNullException(nameof(message)); } TException exception = (TException)Activator.CreateInstance(typeof(TException), message); throw exception; } /// /// 验证指定值的断言表达式是否为真,不为值抛出异常 /// /// /// 要验证的断言表达式 /// 异常消息 public static void Required(T value, Func assertionFunc, string message) { if (assertionFunc == null) { throw new ArgumentNullException(nameof(assertionFunc)); } Require(assertionFunc(value), message); } /// /// 验证指定值的断言表达式是否为真,不为真抛出异常 /// /// 要判断的值的类型 /// 抛出的异常类型 /// 要判断的值 /// 要验证的断言表达式 /// 异常消息 public static void Required(T value, Func assertionFunc, string message) where TException : Exception { if (assertionFunc == null) { throw new ArgumentNullException("assertionFunc"); } Require(assertionFunc(value), message); } /// /// 检查参数不能为空引用,否则抛出异常。 /// /// /// 参数名称 /// public static void NotNull(T value, string paramName) { Require(value != null, string.Format(Resources.ParameterCheck_NotNull, paramName)); } /// /// 检查字符串不能为空引用或空字符串,否则抛出异常或异常。 /// /// /// 参数名称。 /// /// public static void NotNullOrEmpty(string value, string paramName) { Require(!string.IsNullOrEmpty(value), string.Format(Resources.ParameterCheck_NotNullOrEmpty_String, paramName)); } /// /// 检查Guid值不能为Guid.Empty,否则抛出异常。 /// /// /// 参数名称。 /// public static void NotEmpty(Guid value, string paramName) { Require(value != Guid.Empty, string.Format(Resources.ParameterCheck_NotEmpty_Guid, paramName)); } /// /// 检查集合不能为空引用或空集合,否则抛出异常或异常。 /// /// 集合项的类型。 /// /// 参数名称。 /// /// public static void NotNullOrEmpty(IReadOnlyList list, string paramName) { NotNull(list, paramName); Require(list.Any(), string.Format(Resources.ParameterCheck_NotNullOrEmpty_Collection, paramName)); } /// /// 检查集合中没有包含值为null的项 /// public static void HasNoNulls(IReadOnlyList list, string paramName) { NotNull(list, paramName); Require(list.All(m => m != null), string.Format(Resources.ParameterCheck_NotContainsNull_Collection, paramName)); } /// /// 检查参数必须小于[或可等于,参数]指定值,否则抛出异常。 /// /// 参数类型。 /// /// 参数名称。 /// 要比较的值。 /// 是否可等于。 /// public static void LessThan(T value, string paramName, T target, bool canEqual = false) where T : IComparable { bool flag = canEqual ? value.CompareTo(target) <= 0 : value.CompareTo(target) < 0; string format = canEqual ? Resources.ParameterCheck_NotLessThanOrEqual : Resources.ParameterCheck_NotLessThan; Require(flag, string.Format(format, paramName, target)); } /// /// 检查参数必须大于[或可等于,参数]指定值,否则抛出异常。 /// /// 参数类型。 /// /// 参数名称。 /// 要比较的值。 /// 是否可等于。 /// public static void GreaterThan(T value, string paramName, T target, bool canEqual = false) where T : IComparable { bool flag = canEqual ? value.CompareTo(target) >= 0 : value.CompareTo(target) > 0; string format = canEqual ? Resources.ParameterCheck_NotGreaterThanOrEqual : Resources.ParameterCheck_NotGreaterThan; Require(flag, string.Format(format, paramName, target)); } /// /// 检查参数必须在指定范围之间,否则抛出异常。 /// /// 参数类型。 /// /// 参数名称。 /// 比较范围的起始值。 /// 比较范围的结束值。 /// 是否可等于起始值 /// 是否可等于结束值 /// public static void Between(T value, string paramName, T start, T end, bool startEqual = false, bool endEqual = false) where T : IComparable { bool flag = startEqual ? value.CompareTo(start) >= 0 : value.CompareTo(start) > 0; string message = startEqual ? string.Format(Resources.ParameterCheck_Between, paramName, start, end) : string.Format(Resources.ParameterCheck_BetweenNotEqual, paramName, start, end, start); Require(flag, message); flag = endEqual ? value.CompareTo(end) <= 0 : value.CompareTo(end) < 0; message = endEqual ? string.Format(Resources.ParameterCheck_Between, paramName, start, end) : string.Format(Resources.ParameterCheck_BetweenNotEqual, paramName, start, end, end); Require(flag, message); } /// /// 检查指定路径的文件夹必须存在,否则抛出异常。 /// /// /// 参数名称。 /// /// public static void DirectoryExists(string directory, string paramName = null) { NotNull(directory, paramName); Require(Directory.Exists(directory), string.Format(Resources.ParameterCheck_DirectoryNotExists, directory)); } /// /// 检查指定路径的文件必须存在,否则抛出异常。 /// /// /// 参数名称。 /// 当文件路径为null时 /// 当文件路径不存在时 public static void FileExists(string filename, string paramName = null) { NotNull(filename, paramName); Require(File.Exists(filename), string.Format(Resources.ParameterCheck_FileNotExists, filename)); } /// /// 检查各属性的合法性,否则抛出异常 /// public static void Validate(IInputDto dto, string paramName) { NotNull(dto, paramName); dto.Validate(); } /// /// 检查各属性的合法性,否则抛出异常 /// public static void Validate(TInputDto[] dtos, string paramName) where TInputDto : IInputDto { NotNull(dtos, paramName); foreach (TInputDto dto in dtos) { dto.Validate(); } } } }