深度剖析Duwamish 7.0 (1--資料結構)

來源:互聯網
上載者:User
資料|資料結構 不好意思啊,讓大家久等了,第一次寫這種文章,可能沒有開心那麼專業,廢話少說,我們開始:

1.結構概述
Duwamish 7.0 結構分為四個邏輯層:
Web 層
Web 層為用戶端提供對應用程式的訪問。這一層是作為 Duwamish.sln 解決方案檔案中的 Web 項目實現的。Web 層由 ASP.NET Web Form和程式碼後置檔案組成。Web Form只是用 HTML 提供使用者操作,而程式碼後置檔案實現各種控制項的事件處理。

業務外觀層
業務外觀層為 Web 層提供處理帳戶、類別瀏覽和購書的介面。這一層是作為 Duwamish.sln 解決方案檔案中的 BusinessFacade 項目實現的。業務外觀層用作隔離層,它將使用者介面與各種業務功能的實現隔離開來。除了低級系統和支援功能之外,對資料庫伺服器的所有調用都是通過此程式集進行的。

商務規則層
商務規則層是作為 Duwamish.sln 解決方案檔案中的 BusinessRules 項目實現的,它包含各種商務規則和邏輯的實現。商務規則完成如客戶帳戶和書籍訂單的驗證這樣的任務。

資料訪問層
資料訪問層為商務規則層提供資料服務。這一層是作為 Duwamish.sln 解決方案檔案中的 DataAccess 項目實現的。
        
在這裡,對於Duwamish 7.0的分布式結構我就不再羅嗦了,MSDN寫的絕對比我好..

下面就從資料訪問層開始解剖,我單獨提出Customer這一段進行程式講解:

1.CustomerData.cs(資料層)
Code:

//----------------------------------------------------------------
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------

namespace Duwamish7.Common.Data
{
    using System;
    using System.Data;
    using System.Runtime.Serialization;
    
    /// <summary>
    ///     A custom serializable dataset containing customer information.
    ///     <remarks>
    ///         This class is used to define the shape of CustomerData.
    ///     </remarks>
    ///     <remarks>
    ///         The serializale constructor allows objects of type CustomerData to be remoted.
    ///     </remarks>
    /// </summary>
    [SerializableAttribute]
    public class CustomerData : DataSet
    {
        //
        //Customer constants
        //
        /// <value>The constant used for Customers table. </value>
        public const String CUSTOMERS_TABLE = "Customers";
        /// <value>The constant used for Email field in the Customers table. </value>
        public const String EMAIL_FIELD     = "Email";
        /// <value>The constant used for Name field in the Customers table. </value>
        public const String NAME_FIELD      = "Name";
        /// <value>The constant used for Address field in the Customers table. </value>
        public const String ADDRESS_FIELD   = "Address";
        /// <value>The constant used for Country field in the Customers table. </value>
        public const String COUNTRY_FIELD   = "Country";
        /// <value>The constant used for Password field in the Customers table. </value>
        public const String PASSWORD_FIELD  = "Password";
        /// <value>The constant used for PhoneNumber field in the Customers table. </value>
        public const String PHONE_FIELD     = "PhoneNumber";
        /// <value>The constant used for Fax field in the Customers table. </value>
        public const String FAX_FIELD       = "Fax";
        /// <value>The constant used for PKId field in the Customers table. </value>
        public const String PKID_FIELD      = "PKId";
        //
        // Error messages
        //
        /// <value>The constant used for row error when 'Email Not Unique' field in CustomerData. </value>
        public const String EMAIL_FIELD_NOT_UNIQUE     = "Email Not Unique";
        /// <value>The constant used for row error when 'Email Invalid Format' field in CustomerData. </value>
        public const String EMAIL_FIELD_INVALID_FORMAT = "Email Invalid Format";
        /// <value>The constant used for row error when there is an 'Invalid Field' in CustomerData. </value>
        public const String INVALID_FIELD              = "Invalid Field";
        /// <value>The constant used for error when 'Invalid Fields' exist in CustomerData. </value>
        public const String INVALID_FIELDS             = "Invalid Fields";

        /// <summary>
        ///     Constructor to support serialization.
        ///     <remarks>Constructor that supports serialization.</remarks>
        ///     <param name="info">The SerializationInfo object to read from.</param>
        ///     <param name="context">Information on who is calling this method.</param>
        /// </summary>
        public CustomerData(SerializationInfo info, StreamingContext context) : base(info, context)
        {        
        }        
        
        /// <summary>
        ///     Constructor for CustomerData.  
        ///     <remarks>Initialize a CustomerData instance by building the table schema.</remarks>
        /// </summary>
        public CustomerData()
        {
            //
            // Create the tables in the dataset
            //
            BuildDataTables();
        }
                
        //----------------------------------------------------------------
        // Sub BuildDataTables:
        //   Creates the following datatables: Customers
        //----------------------------------------------------------------
        private void BuildDataTables()
        {
            //
            // Create the Customers table
            //
            DataTable         table   = new DataTable(CUSTOMERS_TABLE);
            DataColumnCollection columns = table.Columns;
        
            DataColumn Column = columns.Add(PKID_FIELD, typeof(System.Int32));
            
            Column.AllowDBNull = false;
            Column.AutoIncrement = true;
            
            columns.Add(EMAIL_FIELD, typeof(System.String));
            columns.Add(PASSWORD_FIELD, typeof(System.String));
            columns.Add(NAME_FIELD, typeof(System.String));
            columns.Add(ADDRESS_FIELD, typeof(System.String)).AllowDBNull= false;
            columns.Add(COUNTRY_FIELD, typeof(System.String)).AllowDBNull= false;
            columns.Add(PHONE_FIELD, typeof(System.String)).AllowDBNull= false;
            columns.Add(FAX_FIELD, typeof(System.String));
        
            this.Tables.Add(table);
        }
    
    } //class CustomerData
    
} //namespace Duwamish7.Common.Data

大家可以看到,這裡的CustomerData類繼承與DataSet,詳細定義欄位名稱以及有可能產生的出錯資訊,
並同時繪製出一張空表格。
OK,這裡的代碼非常簡單,大家一看應該就可以明白。代碼的技巧性非常強,以後的資料訪問、交換、傳遞全部通過這裡進行。
私底下認為,採用這種方法有利於團隊協作,代碼編輯也很簡單。
但是,微軟自己也說過dataset的效率沒有datareader高,而dataset的靈活性遠遠超過於datareader,真搞不懂MS在企業案例中為什麼採用Dataset,也許只是讓我們學習其中的思想吧。
過兩天再和大家談談另一種方法,採用datareader,效率高點,但是代碼太難看,難懂:(

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。