漫谈.Net PetShop和Duwamish ADO.NET数据库编程(2)
点击次数:18 次 发布日期:2008-11-27 00:00:33 作者:源代码网
|
源代码网推荐 源代码网推荐 首先,我们来看看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); 源代码网推荐 } 源代码网推荐 ……… 源代码网推荐 } 源代码网推荐 源代码网推荐 我们可以看到它有一个BuildDataTables方法,并且在构造函数中调用,这样,定制的Books表就和这个DataSet捆绑在一起了,省得以后还要进行Column Mapping,这真是个好主意,我怎么就没有想到呢? :) 源代码网推荐 源代码网推荐 解决了数据结构,接下来看看数据层的代码实现,在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; 源代码网推荐 } 源代码网推荐 源代码网推荐 这里就是数据层的代码了,我们在这里可以看到Duwamish采用了DataAdapter来将数据填充到定制的DataSet中,然后返回该DataSet。我感到很奇怪的是在数据存取层中竟然可以看到GetBookById这样具体的数据存取方法,虽然最后还是有一个抽象出来的FillBookData方法,但是上面还有三层啊,底层都做到这份上了,那上层都做些什么呢?答案是数据检查,上层基本上都在做一些很严密的数据合法性校验(当然也会包括一些比较复杂的事务逻辑,但是并不多),示例代码如下: 源代码网推荐 源代码网推荐 源代码网推荐 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; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 在这个方法中,真正进行数据存取的实际上只有 源代码网推荐 源代码网推荐 dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress); 源代码网推荐 源代码网推荐 这么一句,是直接调用的数据层。其它都是在进行合法性校验,我们可以感悟到,进行一个真正的企业级开发需要考虑的系统健壮性有多么重要。 源代码网推荐 源代码网推荐 源代码网供稿. |
