The duwamish bookstore uses the data storage mode used by dataadapter and Dataset. what is different is that it performs subclass extension on dataset as a data carrier, that is, custom dataset is used for data transmission between 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 ));
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 );
}
.........
}
Bind the customized books table With This dataset ..
In duwamish, there are five classes in the data layer: books, categories, MERS MERs, and orders. Each class is only responsible for data access. The following is the sample code of one of the classes:
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;
}
The upper layer basically performs some strict Data Validity verification (of course, some complicated transaction logic will be included, 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 dataset;
}
Else
{
Return NULL;
}
}