1. 在調用方法的時候我們需要對輸入參數進行驗證,譬如非空,只能數字類型等; 2. 一些參數驗證都是非常通用的,所以可以累積後續不斷完善; 3. 這種寫法源於之前很早看到一篇博客後續完善,具體地址忘記了,所以現在Github項目採用協議:MIT,非常抱歉; 4. GitHub地址: "MasterC ...
在調用方法的時候我們需要對輸入參數進行驗證,譬如非空,只能數字類型等;
一些參數驗證都是非常通用的,所以可以累積後續不斷完善;
這種寫法源於之前很早看到一篇博客後續完善,具體地址忘記了,所以現在Github項目採用協議:MIT,非常抱歉;
GitHub地址:MasterChief 參數驗證代碼
歡迎Star,歡迎PR;
1. 代碼實現
using System;
using System.IO;
using System.Text.RegularExpressions;
using MasterChief.DotNet4.Utilities.Common;
using MasterChief.DotNet4.Utilities.Model;
namespace MasterChief.DotNet4.Utilities.Operator
{
/// <summary>
/// 參數驗證幫助類
/// </summary>
public static class ValidateOperator
{
#region Methods
/// <summary>
/// 驗證初始化
/// </summary>
/// <returns>Validation</returns>
public static Validation Begin()
{
return null;
}
/// <summary>
/// 需要驗證的正則表達式
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="checkFactory">委托</param>
/// <param name="pattern">正則表達式</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation Check(this Validation validation, Func<bool> checkFactory, string pattern,
string argumentName)
{
return Check<ArgumentException>(validation, checkFactory,
string.Format(ValidateResource.ParameterCheck_Match2, argumentName));
}
/// <summary>
/// 自定義參數檢查
/// </summary>
/// <typeparam name="TException">泛型</typeparam>
/// <param name="validation">Validation</param>
/// <param name="checkedFactory">委托</param>
/// <param name="message">自定義錯誤消息</param>
/// <returns>Validation</returns>
/// 時間:2016/7/19 11:37
/// 備註:
public static Validation Check<TException>(this Validation validation, Func<bool> checkedFactory,
string message)
where TException : Exception
{
if (checkedFactory())
return validation ?? new Validation
{
IsValid = true
};
var exception = (TException) Activator.CreateInstance(typeof(TException), message);
throw exception;
}
/// <summary>
/// 檢查指定路徑的文件夾必須存在,否則拋出<see cref="DirectoryNotFoundException" />異常。
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">判斷數據</param>
/// <exception cref="ArgumentNullException">ArgumentNullException</exception>
/// <exception cref="DirectoryNotFoundException">DirectoryNotFoundException</exception>
/// <returns>Validation</returns>
public static Validation CheckDirectoryExist(this Validation validation, string data)
{
return Check<DirectoryNotFoundException>(validation, () => Directory.Exists(data),
string.Format(ValidateResource.ParameterCheck_DirectoryNotExists, data));
}
/// <summary>
/// 檢查文件類型
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="actualFileExt">實際文件類型;eg: .xls</param>
/// <param name="expectFileExt">期待文件類型</param>
/// <returns></returns>
public static Validation CheckedFileExt(this Validation validation, string actualFileExt,
string[] expectFileExt)
{
var allowFileExts = expectFileExt.ToString(",");
return Check<FileNotFoundException>(validation, () => expectFileExt.ContainIgnoreCase(actualFileExt),
string.Format(ValidateResource.ParameterCheck_FileExtCompare, allowFileExts));
}
/// <summary>
/// 檢查文件類型
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="actualFileExt">實際文件類型;eg: .xls</param>
/// <param name="expectFileExt">期待文件類型</param>
/// <returns></returns>
public static Validation CheckedFileExt(this Validation validation, string actualFileExt, string expectFileExt)
{
return Check<FileNotFoundException>(validation, () => actualFileExt.CompareIgnoreCase(expectFileExt),
string.Format(ValidateResource.ParameterCheck_FileExtCompare, expectFileExt));
}
/// <summary>
/// 檢查指定路徑的文件必須存在,否則拋出<see cref="FileNotFoundException" />異常。
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">參數</param>
/// <exception cref="ArgumentNullException">當文件路徑為null時</exception>
/// <exception cref="FileNotFoundException">當文件路徑不存在時</exception>
/// <returns>Validation</returns>
public static Validation CheckFileExists(this Validation validation, string data)
{
return Check<FileNotFoundException>(validation, () => File.Exists(data),
string.Format(ValidateResource.ParameterCheck_FileNotExists, data));
}
/// <summary>
/// 檢查參數必須大於[或可等於,參數canEqual]指定值,否則拋出<see cref="ArgumentOutOfRangeException" />異常。
/// </summary>
/// <typeparam name="T">參數類型。</typeparam>
/// <param name="validation">Validation</param>
/// <param name="value">判斷數據</param>
/// <param name="paramName">參數名稱。</param>
/// <param name="target">要比較的值。</param>
/// <param name="canEqual">是否可等於。</param>
/// <exception cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</exception>
/// <returns>Validation</returns>
public static Validation CheckGreaterThan<T>(this Validation validation, T value, string paramName, T target,
bool canEqual)
where T : IComparable<T>
{
// bool flag = canEqual ? value.CompareTo(target) >= 0 : value.CompareTo(target) > 0;
var format = canEqual
? ValidateResource.ParameterCheck_NotGreaterThanOrEqual
: ValidateResource.ParameterCheck_NotGreaterThan;
return Check<ArgumentOutOfRangeException>(validation,
() => canEqual ? value.CompareTo(target) >= 0 : value.CompareTo(target) > 0,
string.Format(format, paramName, target));
}
/// <summary>
/// 檢查參數必須小於[或可等於,參數canEqual]指定值,否則拋出<see cref="ArgumentOutOfRangeException" />異常。
/// </summary>
/// <typeparam name="T">參數類型。</typeparam>
/// <param name="validation">Validation</param>
/// <param name="value">判斷數據</param>
/// <param name="paramName">參數名稱。</param>
/// <param name="target">要比較的值。</param>
/// <param name="canEqual">是否可等於。</param>
/// <exception cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException</exception>
/// <returns>Validation</returns>
public static Validation CheckLessThan<T>(this Validation validation, T value, string paramName, T target,
bool canEqual)
where T : IComparable<T>
{
var format = canEqual
? ValidateResource.ParameterCheck_NotLessThanOrEqual
: ValidateResource.ParameterCheck_NotLessThan;
return Check<ArgumentOutOfRangeException>(validation,
() => canEqual ? value.CompareTo(target) <= 0 : value.CompareTo(target) < 0,
string.Format(format, paramName, target));
}
/// <summary>
/// 驗證是否在範圍內
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">輸入項</param>
/// <param name="min">最小值</param>
/// <param name="max">最大值</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation InRange(this Validation validation, int data, int min, int max, string argumentName)
{
return Check<ArgumentOutOfRangeException>(validation, () => data >= min && data <= max,
string.Format(ValidateResource.ParameterCheck_Between, argumentName, min, max));
}
/// <summary>
/// 是否是中文
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">中文</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsChinses(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsChinses(data), RegexPattern.ChineseCheck, argumentName);
}
/// <summary>
/// 是否是電子郵箱
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="email">需要驗證的郵箱</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsEmail(this Validation validation, string email, string argumentName)
{
return Check(validation, () => CheckHelper.IsEmail(email), RegexPattern.EmailCheck, argumentName);
}
/// <summary>
/// 是否是文件路徑
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">路徑</param>
/// <returns>Validation</returns>
public static Validation IsFilePath(this Validation validation, string data)
{
return Check<ArgumentException>(validation, () => CheckHelper.IsFilePath(data),
string.Format(ValidateResource.ParameterCheck_IsFilePath, data));
}
/// <summary>
/// 是否是十六進位字元串
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">驗證數據</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsHexString(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsHexString(data), RegexPattern.HexStringCheck, argumentName);
}
/// <summary>
/// 是否是身份證號碼
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">驗證數據</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsIdCard(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsIdCard(data), RegexPattern.IdCardCheck, argumentName);
}
/// <summary>
/// 是否是整數
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">需要檢測的字元串</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsInt(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsInt(data), RegexPattern.IntCheck, argumentName);
}
/// <summary>
/// 是否是IP
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">需要檢測到IP</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsIp(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsIp4Address(data), RegexPattern.IpCheck, argumentName);
}
/// <summary>
/// 是否是數字
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">需要檢測的字元串</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsNumber(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsNumber(data), RegexPattern.NumberCheck, argumentName);
}
/// <summary>
/// 是否是合法埠
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">參數值</param>
/// <param name="paramName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsPort(this Validation validation, string data, string paramName)
{
return Check<ArgumentException>(validation, () => CheckHelper.IsValidPort(data),
string.Format(ValidateResource.ParameterCheck_Port, paramName));
}
/// <summary>
/// 是否是郵政編碼
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">郵政編碼</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsPoseCode(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsPoseCode(data), RegexPattern.PostCodeCheck, argumentName);
}
/// <summary>
/// 判斷字元串是否是要求的長度
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="input">驗證的字元串</param>
/// <param name="requireLength">要求的長度</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsRequireLen(this Validation validation, string input, int requireLength,
string argumentName)
{
return Check<ArgumentException>(
validation,
() => input.Length == requireLength,
string.Format(ValidateResource.ParameterCheck_StringLength, argumentName, requireLength));
}
/// <summary>
/// 判斷類型是否能序列化
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">輸入項</param>
/// <returns>Validation</returns>
/// 時間:2016-01-14 9:57
/// 備註:
public static Validation IsSerializable(this Validation validation, object data)
{
return Check<ArgumentException>(validation, () => data.GetType().IsSerializable,
$"該參數類型{data.GetType().FullName}不能序列化!");
}
/// <summary>
/// 是否是URL
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">url</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation IsUrl(this Validation validation, string data, string argumentName)
{
return Check(validation, () => CheckHelper.IsURL(data), RegexPattern.UrlCheck, argumentName);
}
/// <summary>
/// 驗證參數不能等於某個值
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">輸入項</param>
/// <param name="equalObj">比較項</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation NotEqual(this Validation validation, object data, object equalObj, string argumentName)
{
return Check<ArgumentException>(validation, () => data != equalObj,
string.Format(ValidateResource.ParameterCheck_NotEqual, argumentName, data));
}
/// <summary>
/// 驗證非空
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="data">輸入項</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation NotNull(this Validation validation, object data, string argumentName)
{
return Check<ArgumentNullException>(validation, () => CheckHelper.NotNull(data),
string.Format(ValidateResource.ParameterCheck_NotNull, argumentName));
}
/// <summary>
/// 不能為空或者NULL驗證
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="input">輸入項</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation NotNullOrEmpty(this Validation validation, string input, string argumentName)
{
return Check<ArgumentNullException>(validation, () => !string.IsNullOrEmpty(input),
string.Format(ValidateResource.ParameterCheck_NotNullOrEmpty_String, argumentName));
}
/// <summary>
/// 需要驗證的正則表達式
/// </summary>
/// <param name="validation">Validation</param>
/// <param name="input">需要匹配的輸入項</param>
/// <param name="pattern">正則表達式</param>
/// <param name="argumentName">參數名稱</param>
/// <returns>Validation</returns>
public static Validation RegexMatch(this Validation validation, string input, string pattern,
string argumentName)
{
return Check<ArgumentException>(validation, () => Regex.IsMatch(input, pattern),
string.Format(ValidateResource.ParameterCheck_Match, input, argumentName));
}
#endregion Methods
}
}
2. 使用方法
public IEnumerable<T> SqlQuery<T>(string sql, IDbDataParameter[] parameters)
{
ValidateOperator.Begin()
.NotNullOrEmpty(sql, "Sql語句")
.NotNull(parameters, "sql對應參數");
var dataParameters = CreateParameter(parameters);
return CurrentConnection.Query<T>(sql, dataParameters, CurrentTransaction);
}