Duwamish网上电子书店首页

Duwamish书店,它采用的是DataAdapter和DataSet配合的数据存储模式,所不同的是,它对DataSet进行子类化扩展作为数据载体,也就是采用定制的DataSet来进行层间的数据传输,下面是一个定制的DataSet示例:

public class BookData : DataSet
{
  public BookData()
  {
       //
       // Create the tables in the dataset
       //
       BuildDataTables();
  }
  private void BuildDataTables()
  {
        //
        // Create the Books table
        //
        DataTable table   = new DataTable(BOOKS_TABLE);
        DataColumnCollection columns = table.Columns;
       
        columns.Add(PKID_FIELD, typeof(System.Int32));
        columns.Add(TYPE_ID_FIELD, typeof(System.Int32));
        columns.Add(PUBLISHER_ID_FIELD, typeof(System.Int32));
        columns.Add(PUBLICATION_YEAR_FIELD, typeof(System.Int16));
        columns.Add(ISBN_FIELD, typeof(System.String));
        columns.Add(IMAGE_FILE_SPEC_FIELD, typeof(System.String));
        columns.Add(TITLE_FIELD, typeof(System.String));
        columns.Add(DESCRIPTION_FIELD, typeof(System.String));
        columns.Add(UNIT_PRICE_FIELD, typeof(System.Decimal));
        columns.Add(UNIT_COST_FIELD, typeof(System.Decimal));
        columns.Add(ITEM_TYPE_FIELD, typeof(System.String));
        columns.Add(PUBLISHER_NAME_FIELD, typeof(System.String));

        this.Tables.Add(table);
  }
………
}
将定制的Books表就和这个DataSet捆绑在一起..


数据层的代码实现,在Duwamish中,数据层中有5个类,分别是Books,Categories,Customers和Orders,每个类分别只负责有关数据的存取。下面是其中一个类的示例代码:
private SqlDataAdapter dsCommand;
public BookData GetBookById(int bookId)
{
    return FillBookData("GetBookById", "@BookId", bookId.ToString());
}
private BookData FillBookData(String commandText, String paramName, String paramValue)
{
    if (dsCommand == null )
    {
        throw new System.ObjectDisposedException( GetType().FullName );
    }           
    BookData   data    = new BookData();
    SqlCommand command = dsCommand.SelectCommand;

    command.CommandText = commandText;
    command.CommandType = CommandType.StoredProcedure; // use stored proc for perf
    SqlParameter param = new SqlParameter(paramName, SqlDbType.NVarChar, 255);
    param.Value = paramValue;
    command.Parameters.Add(param);           

    dsCommand.Fill(data);
    return data;
}

上层基本上都在做一些很严密的数据合法性校验(当然也会包括一些比较复杂的事务逻辑,但是并不多),示例代码如下:
public CustomerData GetCustomerByEmail(String emailAddress, String password)
{
    //
    // Check preconditions
    //
    ApplicationAssert.CheckCondition(emailAddress != String.Empty, "Email address is required",
ApplicationAssert.LineNumber);
    ApplicationAssert.CheckCondition(password != String.Empty, "Password is required",
ApplicationAssert.LineNumber);
    //
    // Get the customer dataSet
    //
    CustomerData dataSet;
    using (DataAccess.Customers customersDataAccess = new DataAccess.Customers())
    {
dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress);
    }
    //   
    // Verify the customer's password
    //
    DataRowCollection rows = dataSet.Tables[CustomerData.CUSTOMERS_TABLE].Rows;

    if ( ( rows.Count == 1 ) && rows[0][CustomerData.PASSWORD_FIELD].Equals(password) )
    {
        return dataSet;
    }
    else
    {       
return null;
    }
}
posted on 2005-11-18 13:21  泽来  阅读(656)  评论(0编辑  收藏  举报