3

我有下面的方法,我需要检查参数是空的还是空的。

    public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
    {
        _Where = " WHERE " + field + " " + operat + " @" + field + "1 " + andOr + " " + field2 + " " + operat2 + " @" + field2 + "2 ";
        _Params.Add(field + "1", value);
        _Params.Add(field2 + "2", value2);
        return this;
    }

我找到了 string.IsNullOrWhiteSpace 方法,但这需要这么多代码:

                   if (string.IsNullOrWhiteSpace(field))
            throw new ArgumentException("field Cannot be null or be empty");

        if (string.IsNullOrWhiteSpace(operat))
            throw new ArgumentException("operat Cannot be null or be empty");

        if (string.IsNullOrWhiteSpace(value))
            throw new ArgumentException("value Cannot be null or be empty");

        if (string.IsNullOrWhiteSpace(andOr))
            throw new ArgumentException("andOr Cannot be null or be empty");

        if (string.IsNullOrWhiteSpace(field2))
            throw new ArgumentException("field2 Cannot be null or be empty");

        if (string.IsNullOrWhiteSpace(operat2))
            throw new ArgumentException("operat2 Cannot be null or be empty");

        if (string.IsNullOrWhiteSpace(value2))
            throw new ArgumentException("value2 Cannot be null or be empty");

有没有办法缩短这个?

另外,我尝试为这个任务创建一个自定义方法,但是它在自定义方法而不是 Where() 方法中引发了一个异常,这使得调试变得很棘手。

4

5 回答 5

4

您可以一一检查该值或创建中间函数来执行此操作。

或者,我的建议是:您可以将所有输入放在一个数组中并使用 LINQ Any 一次检查所有输入:

public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
    string[] inputs = {field, operat, value, andOr, field2, operat2, value2}
    if (inputs.Any(x => string.IsNullOrWhiteSpace(x))){
        //throw exception
    }
    //continue with your method, all inputs are OK
}
于 2016-03-05T17:24:35.810 回答
1

我可以建议的是:

private string _nullChecker(string _value){
   if (string.IsNullOrWhiteSpace(_value))
            throw new ArgumentException(_value + "Cannot be null or be   empty");
   return _value;
}

然后,在您的 Where 字符串语句中

_Where = " WHERE " + _nullChecker(field) + " " + __nullChecker(operat) + " @" + _nullChecker(field) + "1 " + _nullChecker(andOr) + " " + _nullChecker(field2) + " " + _nullChecker(operat2) + " @" + _nullChecker(field2) + "2 ";

不过不确定。没有用实际代码检查它。:) 希望这可以帮助

于 2016-03-05T17:16:24.123 回答
0

你可以这样做:

int? GetLength(string s) {
    return s == "" ? -1 : s?.Length;
}

// s1, s2 and so on are your parameters
int? lengthSum = GetLength(s1) + GetLength(s2); // and so on
int wholeLength = (s1 + s2).Length; // and so on
if(lengthSum == wholeLength) {
    // No parameter is null or empty
}
于 2016-03-05T17:35:48.063 回答
0

我想我找到了你正在做的事情的“分支”。下面要检查的代码较少,并且在函数内引发了错误。

Adam Storr 有一篇很棒的文章,关于参数的检查here

对于懒惰的人,您会发现使用扩展方法,允许在 try/catch 内的一行中检查参数。

它允许独立检查所有参数,并提供自定义反馈

public returnType MyFunction(string arg1, string arg2, int arg3)
{
    try
    {
        // Check that the arguments are not null or empty
        _ = arg1.Valid() ?? throw new ArgumentNullException("Argument 1 not valid");
        _ = arg2.Valid() ?? throw new ArgumentNullException("The argument 2 must be provided");
        _ = arg3.Valid() ?? throw new ArgumentNullException("I need arg3 to work");
    }
    catch(ArgumentNullException)
    {
    //Treat the error
    }

并且 .Valid() 方法的实现发生在您想要的另一个文件位置(在这种情况下存储在 TypeExtension 文件中):

 public static class TypeExtensions
    {
        /// <summary>
        /// String method extension that return null if the string is null or empty.<br/>
        /// Otherwise return the value itself
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Valid(this string value)
        {
            return (!string.IsNullOrWhiteSpace(value)) ? value : null;
        }

        /// <summary>
        /// int method extension that return null if the int is null or 0 <br/>
        /// Otherwise return the value itself
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Valid(this int? value)
        {
            if (value is null or 0)
                return null;
            else
                return value;
        }
    }

因此,方法 Valid() 的参数按其类型继承。“null-coalescing operator”(??)表示“如果值为null,则给出下一个”丢弃“_”表示无论返回什么,它都不会存储在内存中。

因此,通过两者的关联,您可以检查参数,如果检查返回 null,则抛出异常。

我同意这是为了扭曲的头脑,但到目前为止我还没有找到“更清洁”的方式。也许与装饰者?

我希望它可能会有所帮助!

于 2021-09-07T13:16:45.850 回答
0

首先,您可以使用简单的库来进行参数验证。查看这个名为Argument Validator的工具,它具有方便的功能,可以将您的整体代码减少一半。

下面是一个关于如何使用参数验证器库的示例:

public DB Where(string field, string operat, string value, string andOr, string field2, string operat2, string value2)
{
    var inputs = new string[] {field, operat, value, andOr, field2, operat2, value2};
    foreach(var input in inputs)
    {
        Throw.IfNullOrEmpty(input, nameof(input)));
    }
}    
于 2016-08-21T00:30:19.790 回答