如何将语句重构为一个语句

时间:2014-02-28 13:24:45

标签: c# asp.net-mvc

    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");
}

我想重构一下,只使用一个if语句。 如果(“任何记录为空”) { 抛出新的异常(“rror创建顺序”,记录“不能为空 }

感谢

4 个答案:

答案 0 :(得分:3)

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

这会在3个条件之间创建or语句。如果它们中的任何一个发生,那么将抛出异常。

然而,这种重构对我来说没有多大意义。保留代码可能是一个更好的主意,为用户提供更具描述性的错误消息。

答案 1 :(得分:0)

正如我在评论中建议的那样,这就是你的成就:

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

但如果所有类型(被比较)相似,那么您可以使用null-coalescing运算符,如下所示:

注意:下面的示例仅在所有三种类型都相似的情况下才有效。

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

答案 2 :(得分:0)

我不认为抛出businessrule验证的例外是最好的想法,但是如果你必须密切关注你所拥有的东西,你可以使用FirstOrDefault的扩展方法,并将自定义异常消息作为参数。

这是扩展方法:

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;
    }
}

您当前的代码将被重构为:

 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");

答案 3 :(得分:0)

最好留下三个陈述,但要早点退回。

因此,在每次操作之后立即移动每个语句:

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");
}