是否有太多if else语句验证不好?

时间:2013-08-29 12:31:21

标签: c# .net visual-studio-2010

来自专业企业.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- if else语句太多了。 (为什么不切换?)

2-该类都充当实体,并且具有验证。这不是一个臭设计吗? (这种书中的单一责任原则违规?)

也许我错了,但我不想从一本应该教授企业设计的书中汲取不良做法。本书充满了类似的代码片段,现在真的很烦我。如果设计不好,怎么能绕过太多if else语句?

显然,我不希望你重写这个课程,只是对如何完成它的一般概念。

谢谢。

2 个答案:

答案 0 :(得分:3)

  

if else语句太多

如果需要,那么没有任何问题。检查比捕获异常更好

  

该类都充当实体,AND具有验证

如果要将某些内容加载到实体上,则需要进行验证。

我快速浏览了一下课程,对我来说很好看。

答案 1 :(得分:3)

不,没有错。虽然我可以看到其他编写方法,但第一眼看上去的代码并不清晰。