2013-08-29 51 views
-1

來自專業企業.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-該類既作爲一個實體,也有驗證。這是不是一個臭的設計? (這種書違反單一責任原則?)

也許我錯了,但我不想從一本應該教企業設計的書中撿到不好的做法。本書充滿了類似的代碼片段,它現在真的讓我感到困擾。如果這是不好的設計,那麼如何避免太多的陳述?

顯然,我不指望你重寫這個類,只是它應該如何完成的一般概念。

謝謝。

+0

如何使用switch()? –

+2

您將使用'switch'比較** one **屬性的值,而不是許多屬性的值。 –

+2

您可以用開關替換if/else並不總是可能的。 – Ehsan

回答

3

如果有太多的else語句

如果需要,那麼就沒有什麼錯。這是更好地檢查,而不是catch異常

類都作爲一個實體,並已確認

如果要加載的東西到實體則需要驗證。

我已經快速瀏覽了課程,對我來說看起來不錯。

+0

是驗證是必需的,但是在單一責任主體之後,應該有一個單獨的驗證對象 –

+0

@iAteABug_And_iLiked_it不根據StackOverflow:http://stackoverflow.com/a/2223606/96780。 –

3

不,沒什麼問題。雖然我可以看到其他寫作方法,但乍看之下,代碼的清晰度和清晰度都不盡如人意。