来自《Professional Enterprise .Net》一书,该书在亚马逊上获得 5 星评级,我在阅读后对此表示怀疑。这是一个 Borrower 类(这不是侵犯版权,完整的代码也可以从 Wrox 网站获得):
using System;
using System.Collections.Generic;
using ProEnt.LoanPrequalification.Model.LoanApplications;
namespace ProEnt.LoanPrequalification.Model.Borrowers
{
public class Borrower
{
private Guid _id;
private int _age;
private string _firstName;
private string _lastName;
private IAddress _contactAddress;
private IBankAccount _bankAccount;
private CreditScore _creditScore;
private Employer _employer;
private LoanApplication _loanApplication;
public Guid Id
{
get { return _id; }
set { _id = value; }
}
public LoanApplication LoanApplication
{
get { return _loanApplication; }
set { _loanApplication = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public IAddress ContactAddress
{
get { return _contactAddress; }
set { _contactAddress = value; }
}
public Employer Employer
{
get { return _employer; }
set { _employer = value; }
}
public IBankAccount BankAccount
{
get { return _bankAccount; }
set { _bankAccount = value; }
}
public CreditScore CreditScore
{
get { return _creditScore; }
set { _creditScore = value; }
}
public List<BrokenBusinessRule> GetBrokenRules()
{
List<BrokenBusinessRule> brokenRules = new List<BrokenBusinessRule>();
if (Age < 18)
brokenRules.Add(new BrokenBusinessRule("Age", "A borrower must be over 18 years of age"));
if (String.IsNullOrEmpty(FirstName))
brokenRules.Add(new BrokenBusinessRule("FirstName", "A borrower must have a first name"));
if (String.IsNullOrEmpty(LastName))
brokenRules.Add(new BrokenBusinessRule("LastName", "A borrower must have a last name"));
if (CreditScore == null)
brokenRules.Add(new BrokenBusinessRule("CreditScore", "A borrower must have a credit score"));
else if (CreditScore.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, CreditScore.GetBrokenRules());
}
if (BankAccount == null)
brokenRules.Add(new BrokenBusinessRule("BankAccount", "A borrower must have a bank account defined"));
else if (BankAccount.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, BankAccount.GetBrokenRules());
}
if (Employer == null)
brokenRules.Add(new BrokenBusinessRule("Employer", "A borrower must have an employer"));
else if (Employer.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, Employer.GetBrokenRules());
}
if (ContactAddress == null)
brokenRules.Add(new BrokenBusinessRule("ContactAddress", "A borrower must have a bank account defined"));
else if (ContactAddress.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, ContactAddress.GetBrokenRules());
}
return brokenRules;
}
private void AddToBrokenRulesList(List<BrokenBusinessRule> currentBrokenRules, List<BrokenBusinessRule> brokenRulesToAdd)
{
foreach (BrokenBusinessRule brokenRule in brokenRulesToAdd)
{
currentBrokenRules.Add(brokenRule);
}
}
}
}
此类是作者构建的 Mortgage 应用程序的一部分。
令我困惑的是,这本书应该是关于专业企业设计的。
我不是专家,但我觉得不舒服
1- if else 语句太多。(为什么不切换?)
2-该类都用作实体,并且具有验证。不是很臭的设计吗?(这种书违反了单一责任原则?)
也许我错了,但我不想从一本本应教授企业设计的书中汲取不良做法。书中充满了类似的代码片段,现在真的让我很烦。如果它是糟糕的设计,如何绕过太多的 if else 语句?
显然,我不希望你重写这个类,只是对它应该如何做的一个大致的想法。
谢谢你。