数据|数据结构
不好意思啊,让大家久等了,第一次写这种文章,可能没有开心那么专业,废话少说,我们开始:
1.结构概述
Duwamish 7.0 结构分为四个逻辑层:
Web 层
Web 层为客户端提供对应用程序的访问。这一层是作为 Duwamish.sln 解决方案文件中的 Web 项目实现的。Web 层由 ASP.NET Web 窗体和代码隐藏文件组成。Web 窗体只是用 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,效率高点,但是代码太难看,难懂:(