企业代码需要我们用以下方式重新看待代码

一、模块性   

代码单元通常表现为类或类型。它们是基于特定目标而设计的。各个代码单元间的交互方式既要能达到较大的期望目标,    又不能违背对它们进行划分的准则。模块化不仅是针对可重用性的代码分离,同时也要求很强的松散耦合度。

二、松散耦合的类    

如果代码单元需要使用来自系统其他部分的服务,那么这些服务应该抽象地由传递到该单元中。创建所需的依懒不应该是该单元的职责。  如果针对代码单元编写单元测试很方便,就证明其松散耦合度很低。

看下面的示例:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Web;

namespace ProfessionalEnterprise.Net.Chapter02
{
    public class Program
    {
        const double basePrice = 35.00;
        static void Main(string[] args) {
            DataSet ds = new DataSet();
            OleDbCommand cmd = new OleDbCommand();
            string sql = "Select c.LastName,c.FristName,c.MiddleName,c.CustmerRegion,e.EmailAddress from Customers c inner join Email e on c.ID = e.CustomerID Order by c.LastName ASC";
            string name = "";
            double price = 0.0;
            OleDbConnection conn = new OleDbConnection();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            adapter.SelectCommand = cmd;

            try
            {
                adapter.Fill(ds);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
            }

            foreach (DataRow dr in ds.Tables[0].Rows) {
                name = String.Format("{0},{1}{2}", dr["LastName"], dr["FirstName"], dr["MiddleName"]);
                switch (dr["CustomerRegion"].ToString()) { 
                    case "1":
                        price = (0.9 * basePrice);
                        break;
                    case "2":
                        price = (0.85 * basePrice);
                        break;
                    case "3":
                        price = (0.8 * basePrice);
                        break;
                }

                Console.WriteLine(String.Format("Customer name:{0} Customer Email Address:{1} Customer's Price:{3}", name, dr["EmailAddress"].ToString(), price));
            }
        }

    }
}

Program中的代码可以正常工作,但不是良好设计的代码。它不仅严重违背了模块化规则,还违背了松散耦合的基本准则。  在本例中,我们无法编写单元测试。

 对于同一示例,一个好的多的版本可能要分解为不同的类型,每个类型能够符合模块化和松散耦合的规则。

首先,我们创建一个简单的数据实用工具类型:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Web;

namespace ProfessionalEnterprise.Net.Chapter02
{
    /// <summary>
    /// 一个简单的数据实用工具类型
    /// 执行简单的数据提取,并以ADO.NET数据行集的形式返回提取的数据
    /// </summary>
    public class DataUtility
    {
        private string _connectionString;
        private DataSet _ds;
        private OleDbCommand _cmd;
        private OleDbConnection _conn;
        private OleDbDataAdapter _adapter;

        public DataUtility(string connectionString) {
            _connectionString = connectionString;
        }

        public DataRowCollection GetData(string sql) {
            _ds = new DataSet();
            _conn = new OleDbConnection(_connectionString);
            _cmd = new OleDbCommand();
            _cmd.Connection = _conn;
            _cmd.CommandType = CommandType.Text;
            _cmd.CommandText = sql;
            _adapter = new OleDbDataAdapter(_cmd);

            try
            {
                _adapter.Fill(_ds);
            }
            catch 
            { 
               //handle exception and log the event
            }

            return _ds.Tables[0].Rows;
        }
    }
}
View Code

相关文章: