如何避免重复if else系列代码的味道

时间:2017-02-07 18:16:21

标签: c# code-cleanup

使用C#4.5。我的波纹管语法如此之多,如果其他系列似乎代码味道,想要一种方法来避免这种气味。任何一种帮助都是可以接受的。谢谢

public bool CheckValidCustomer()
{
    return _checkManager.IsCustomerPersonal(_customer) ? IsValidPersonalCustomer() : IsValidCompanyCustomer();
}

private bool IsValidCompanyCustomer()
{
    if (_checkManager.IsValidFinancialInfo(_customer) == false)
    {
        ProcessMessage = "Please Check Financial Info.";
        return false;
    }

    if (_checkManager.IsValidCompanyInfo(_customer) == false)
    {
        ProcessMessage = "Please Check Company Info.";
        return false;
    }

    if (_checkManager.IsValidStakeHolderInfo(_customer) == false)
    {
        ProcessMessage = "Please Check Stake Holder Info.";
        return false;
    }

    if (_checkManager.IsValidResponsiblePersonInfo(_customer) == false)
    {
        ProcessMessage = "Please Check Responsible person Info.";
        return false;
    }


    if (_checkManager.IsValidScreeningInfo(_customer) == false)
    {
        ProcessMessage = "Please Check Screening Info .";
        return false;
    }

    if (_checkManager.IsValidMyNumberUpload(_customer) == false)
    {
        ProcessMessage = "Please Check My Number Upload Info.";
        return false;
    }

    if (_checkManager.IsValidIdUpload(_customer) == false)
    {
        ProcessMessage = "Please Check Id Upload Status.";
        return false;
    }

    if (_checkManager.IsValidCustomerStatus(_customer) == false)
    {
        ProcessMessage = "Please Check Customer Status.";
        return false;
    }

    return true;
}

private bool IsValidPersonalCustomer()
{
    if (_checkManager.IsValidPersonalInfo(_customer)==false)
    {
        ProcessMessage = "Please Check Personal Info.";
        return false;
    }

    if (_checkManager.IsValidFinancialInfo(_customer)==false)
    {
        ProcessMessage = "Please Check Financial Info.";
        return false;
    }

    if (_checkManager.IsValidCompanyInfo(_customer)==false)
    {
        ProcessMessage = "Please Check Company Info.";
        return false;
    }

    return true;
}

3 个答案:

答案 0 :(得分:3)

您可以使用验证规则模式。 Avoiding many if blocks for validation checking

创建一组验证规则。并逐一贯穿所有这些。如果任何一个验证规则失败,则完成验证失败(取决于业务规则)。

您还可以参考WPF Data Binding Validation(数据验证 - >验证流程部分)获取设计自己的验证规则引擎的建议。

答案 1 :(得分:2)

我的个人建议:

  1. 创建基类Customer和两个子类PersonalCustomer / CompanyCustomer。验证应该在这些类中完成 - 他们知道它们的实现细节。
  2. 客户基类有一个成员,它是一系列验证操作,链中的每个操作项都返回一个Enum作为验证结果(成功o4特定错误)
  3. 客户基类有一个方法Validate,它调用链中的每个验证操作,如果验证失败则返回
  4. 每个子类都实现详细的验证操作,并将其注册到验证链中。没有更多的checkmanager。调用者只需调用_customer.Validate(),就不必担心客户的类型。
  5. 一个表示层,用于将枚举(基本上是错误代码)映射到某些特定于UI的字符串,这可以通过数组/ hashset完成 - 不再是if / else或switch。

答案 2 :(得分:0)

你可以这样做......

private bool IsValidCompanyCustomer()
{
    var companyValidationRules = new Dictionary<string, Func<Customer, bool>>
    {
        { "Please Check Financial Info.", _checkManager.IsValidFinancialInfo},
        { "Please Check Company Info.", _checkManager.IsValidCompanyInfo},
        { "Please Check Stake Holder Info.", _checkManager.IsValidStakeHolderInfo},
        { "Please Check Responsible person Info.", _checkManager.IsValidResponsiblePersonInfo},
        { "Please Check Screening Info.", _checkManager.IsValidScreeningInfo},
        { "Please Check My Number Upload Info.", _checkManager.IsValidMyNumberUpload},
        { "Please Check Id Upload Status.", _checkManager.IsValidIdUpload},
        { "Please Check Customer Status.", _checkManager.IsValidCustomerStatus},
    };

    var failedRule = companyValidationRules.Where(d => !d.Value(_customer))
                                           .Select(d => d.Key)
                                           .FirstOrDefault();
    if (!string.IsNullOrWhiteSpace(failedRule))
    {
        this.ProcessMessage = failedRule;
        return false;
    }

    return true;
}

private bool IsValidPersonalCustomer()
{
    var companyValidationRules = new Dictionary<string, Func<Customer, bool>>
    {
        { "Please Check Personal Info.", _checkManager.IsValidPersonalInfo},
        { "Please Check Financial Info.", _checkManager.IsValidFinancialInfo},
        { "Please Check Company Info.", _checkManager.IsValidCompanyInfo},
    };

    var failedRule = companyValidationRules.Where(d => !d.Value(_customer))
                                           .Select(d => d.Key)
                                           .FirstOrDefault();
    if (!string.IsNullOrWhiteSpace(failedRule))
    {
        this.ProcessMessage = failedRule;
        return false;
    }

    return true;
}

}