参数验证一种方式
程序员文章站
2022-06-28 20:38:48
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); }
上一篇: C# 虚拟串口通信
下一篇: 基于xcache的配置与使用详解