Avoiding repetition if the smell of else series code

Work on C # 4.5. My single syntax is so many, if in other series it seems that the smell of code, you need a way to avoid this smell. Any help would be acceptable. Thanks you

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;
}
+4
source share
3 answers

You can use the validation rule template. Avoid many if blocks for validation

Create a set of validation rules. And run them one at a time. If any one validation rule fails, a full validation fails (depending on business rules).

WPF ( → ), .

+3

:

  • PersonalCustomer/CompanyCustomer. - .
  • Customer , , Enum ( o4 )
  • Customer Validate, , .
  • . checkmanager. _customer.Validate(), , .
  • ( ) UI, /hashset - if/else switch.
+2

You can do something like this ...

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

}

0
source

Source: https://habr.com/ru/post/1669068/


All Articles