來自專業企業.Net,它在亞馬遜上有5星級的評價,這是我通過閱讀後所懷疑的。這裏是一個借款人的類(這是不是侵犯版權,完整的代碼,也可以從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);
}
}
}
}
這個類是抵押貸款申請,作者構建的一部分。
讓我感到困惑的是,本書應該是關於專業企業設計的。
我不是專家,但我不覺得舒服
1-如果有太多的else語句。 (爲什麼不切換?)
2-該類既作爲一個實體,也有驗證。這是不是一個臭的設計? (這種書違反單一責任原則?)
也許我錯了,但我不想從一本應該教企業設計的書中撿到不好的做法。本書充滿了類似的代碼片段,它現在真的讓我感到困擾。如果這是不好的設計,那麼如何避免太多的陳述?
顯然,我不指望你重寫這個類,只是它應該如何完成的一般概念。
謝謝。
如何使用switch()? –
您將使用'switch'比較** one **屬性的值,而不是許多屬性的值。 –
您可以用開關替換if/else並不總是可能的。 – Ehsan