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

最佳答案

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

创建一组验证规则。并将它们一一贯穿。如果任一验证规则失败,则完整验证失败(取决于业务规则)。

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

关于c# - 如何避免重复 if else 系列代码异味,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42097054/

10-17 01:43