使用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;
}
答案 0 :(得分:3)
您可以使用验证规则模式。 Avoiding many if blocks for validation checking
创建一组验证规则。并逐一贯穿所有这些。如果任何一个验证规则失败,则完成验证失败(取决于业务规则)。
您还可以参考WPF Data Binding Validation(数据验证 - >验证流程部分)获取设计自己的验证规则引擎的建议。
答案 1 :(得分:2)
我的个人建议:
答案 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;
}
}