Write .Net PetShop and Duwamish ADO.NET Database Programming

zhaozj2021-02-16  76

2002-10-18 · 卢 彦 ·· ASP.NET Chinese Professional Network

Duwamish Data Access Analysis First, let's take a look at the Duwamish bookstore. It uses the data storage mode of DataAdapter and DataSet, which is different. It is a data carrier for subcatenification extensions for DataSet, which is customized DataSet. Data transmission between the layers, the following is a custom DataSet example:

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)); Column.Add (Description_Field, Type (System.String)); Column.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) } .........}

We can see it has a buildDataTables method, and in the constructor, this, the custom Books table is bundled with this Dataset, and saving column mapping after saving, this is a good idea, how can I not Think? :) Solved the data structure, then look at the code implementation of the data layer, in Duwamish, 5 classes in the data layer, Books, Categories, Customers, and Orders, each class is only responsible for access to the data . The following sample code is a class wherein: 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;}

Here is the code of the data layer, we can see that duwamish uses DataAdapter to populate the data into the custom DataSet and then return the Dataset. I am very strange that it can see the specific data access method of getbookbyid in the data access layer. Although there is still an abstract FillBookData method, there are three layers above, the bottom is doing this What do you do? The answer is data inspection, and the upper layer is basically doing some very strict data legitimacy checks (of course, there will be some complicated transaction logic, but not much), the sample code is as follows:

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 DA Taset;}}}}} In this method, only DataSet = CustomersDataAccess.LoadCustomerbyemail (EmailAddress); this is the data layer that is directly called. All other are conducting legitimate checks, we can understand how important the system robustness needs to be considered for a real enterprise development.

转载请注明原文地址:https://www.9cbs.com/read-16855.html

New Post(0)