简体   繁体   English

如何将if语句重构为一条语句

[英]how to refactor if statements into one statement

    PROJ_ClientAccount Client = db.PROJ_ClientAccount.Where(x => x.Id == 1).FirstOrDefault();
    PROJ__VATRateRecord VatRecord = db.PROJ_VATRateRecord.Where(x => x.Id == 2).FirstOrDefault();
    PROJ__ProductRecord ProductRecord = db.PROJ_ProductRecord.Where(x => x.Id == sale.Value.ProductId).FirstOrDefault();

if (Client == null)
{
throw new Exception("Error creating new Order Record, Client Account can't be empty");
}

if (VatRecord == null)
{
throw new Exception("Error creating new Order Record, VAT can't be empty");
}

if (ProductRecord == null)
{
throw new Exception("Error creating new Order Line Record, ProductRecord can't be empty");
}

I would like to refactor this and only use one if statement. 我想重构它,仅使用一个if语句。 If ("any of record is null") { throw new exception("rror creating order, "record" cant be empty } 如果(“任何记录为空”){引发新异常(“创建顺序错误,“记录”不能为空}

thanks 谢谢

if (Client == null || VatRecord == null || ProductRecord == null)
{
    throw new Exception("Error creating new Order, \"record\" cannot be empty");
}

This creates an or statement between the 3 conditions. 这将在3个条件之间创建一个or语句。 If any one of them occurs, then the exception will be thrown. 如果其中任何一个发生,则将引发异常。

However, this refactoring doesn't make much sense to me. 但是,这种重构对我来说没有多大意义。 It is probably a better idea to leave the code as you had it, to provide more descriptive error messages for the user. 最好保留代码不变,为用户提供更多描述性的错误消息。

as i suggested you in my comments this is how you achieve: 正如我在评论中建议您的那样,这就是您如何实现的:

if (Client == null || VatRecord == null || ProductRecord == null)
{
   throw new Exception("Error creating new Order,Record can't be empty");
}

but if all of the types(being compared) are similar then you can use null-coalescing operator as below: 但是,如果所有类型(进行比较)都相似,则可以使用null-coalescing运算符,如下所示:

Note: below example works only if all of the three types are similar. 注意:以下示例仅在所有三种类型都相似时才起作用。

var obj = Client ?? VatRecord ?? ProductRecord ?? null;
if(obj == null)
{
throw new Exception("Error creating new OrderRecord can't be empty");
}

I don't think throwing exceptions for businessrule validation is the best idea but if you have to stick closely to what you have you could make use of an extension method for FirstOrDefault with a custom exception message as parameter. 我不认为抛出异常进行业务规则验证是最好的主意,但是如果您必须坚持自己的想法,则可以使用FirstOrDefault的扩展方法,并使用自定义异常消息作为参数。

This is the extension method: 这是扩展方法:

public static class BusinessValidator
{
    public static TSource FirstOrDefault<TSource>(
        this IEnumerable<TSource> source, string message)
    {
        TSource src = source.FirstOrDefault();
        if (src == null)
        {
            throw new Exception(message);
        }
        return src;
    }
}

Your current code wil be refactored to: 您当前的代码将被重构为:

 PROJ_ClientAccount Client = db
                   .PROJ_ClientAccount
                   .Where(x => x.Id == 1)
                   .FirstOrDefault("Error creating new Order Record, Client Account can't be empty");
PROJ__VATRateRecord VatRecord = db
                   .PROJ_VATRateRecord
                   .Where(x => x.Id == 2)
                   .FirstOrDefault("Error creating new Order Record, VAT can't be empty");
PROJ__ProductRecord ProductRecord = db
                   .PROJ_ProductRecord
                   .Where(x => x.Id == sale.Value.ProductId)
                   .FirstOrDefault("Error creating new Order Line Record, ProductRecord can't be empty");

It is better to leave three statements as is but make fall back earlier. 最好保留三个语句,但要早退。

So move each statement just right after each operation like this: 因此,像这样在每个操作之后立即移动每个语句:

PROJ_ClientAccount Client = db.PROJ_ClientAccount.Where(x => x.Id == 1).FirstOrDefault();

if (Client == null)
{
throw new Exception("Error creating new Order Record, Client Account can't be empty");
}

PROJ__VATRateRecord VatRecord = db.PROJ_VATRateRecord.Where(x => x.Id == 2).FirstOrDefault();

if (VatRecord == null)
{
throw new Exception("Error creating new Order Record, VAT can't be empty");
}

PROJ__ProductRecord ProductRecord = db.PROJ_ProductRecord.Where(x => x.Id == sale.Value.ProductId).FirstOrDefault();

if (ProductRecord == null)
{
throw new Exception("Error creating new Order Line Record, ProductRecord can't be empty");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM