Learn PetShop3.0 (2) pet display

Source: Internet
Author: User
Pet display, that is, product display (Khan ......)
The pages that involve this topic include Category. aspx/Items. aspx/ItemDetails. aspx, which are divided into three categories, classes, and details. The following is an analysis
It should be noted that, as mentioned in the first article, data transmission is completed by directly transferring business entities. Is there a strong object-oriented taste?
Category. aspx
The main body of the page is a user-defined control: SimplePager, which inherits from Repeater and stores pet information. Its data source acquisition method uses the. net cache api. The specific implementation code is:
If (Cache [categoryKey]! = Null ){
// If the data is already cached, then used the cached copy
Products. DataSource = (IList) Cache [categoryKey];
} Else {
// If the data is not cached, then create a new products object and request the data
Product product = new Product ();
IList productsByCategory = product. GetProductsByCategory (categoryKey );
// Store the results of the call in the Cache and set the time out to 12 hours
Cache. Add (categoryKey, productsByCategory, null, DateTime. Now. AddHours (12), Cache. nosdomainingexpiration, CacheItemPriority. High, null );
Products. DataSource = productsByCategory;
}
// Bind the data to the control
Products. DataBind ();
This is done using a common approach. New data is obtained through the PetShop. BLL. Product. GetProductsByCategory method, and analyzed later. After SimplePager obtains the data, it displays the data according to the template settings just like a normal Repeater. The Analysis of SimplePager is also followed.
After clicking on a specific small class, the user enters the page of the small class and transmits the number of the small class through the url.
Items. aspx
It is basically the same as the page above, because all operations are performed on the category.
Select a specific pet and go to the details page.
ItemDetails. aspx
This page does not use cache, but directly retrieves records from the database through the business logic layer, and then returns to the presentation layer. It's easy to understand at a glance.
Let's take a look at the SimplePager control.
Many Repeater methods are rewritten in the definition of the control. The main purpose is to have the paging function. Let's take a look at the Render method.
PetShop. Web. Controls. SimplePager: Repeater
Override protected void Render (HtmlTextWriter writer ){

// Check there is some data attached
If (ItemCount = 0 ){
Writer. Write (emptyText );
Return;
}

// Mask the query
String query = Context. Request. Url. Query. Replace (COMMA, AMP );
Query = RX. Replace (query, string. Empty );

// Write out the first part of the control, the table header
Writer. Write (HTML1 );

// Call the inherited method
Base. Render (writer );

// Starting from here, this is the key part of rewriting. It is used to display the buttons on the top and bottom pages.
// Write out a table row closure
Writer. Write (HTML2 );

// Determin whether next and previous buttons are required
// Previous button?
If (currentPageIndex> 0)
Writer. Write (string. Format (LINK_PREV, (currentPageIndex-1) + query ));

// Other .....

}

In addition, this control also has a Custom Event PageIndexChanged to respond to page switching. DataSource accepts the data that implements the IList interface, and we can see the statements used when the above large page is displayed.
Products. DataSource = productsByCategory;
As the return value of a method of the business logic component, productsByCategory implements the data of the IList interface.
Next, let's see how the data is obtained.
The basic process starts from the final data operation component, so we can directly look at the final data operation part.
PetShop. SQLServerDAL. Product: IProduct
Public ilist getproductsbycategory (string category ){

Ilist productsbycategory = new arraylist ();

Sqlparameter parm = new sqlparameter (parm_category, sqldbtype. Char, 10 );
Parm. value = category;

// Execute a query to read the products
Using (sqldatareader RDR = sqlhelper. executereader (sqlhelper. conn_string_non_dtc, commandtype. Text, SQL _select_products_by_category, parm )){
While (RDR. Read ()){
// Add an entire productinfo object to arraylist
Productinfo Product = new productinfo (RDR. getstring (0), RDR. getstring (1), null );
Productsbycategory. Add (product );
}
}

Return productsbycategory;
}

Obviously, an arraylist is returned. This contains information about all the small classes in this category. A small class is a productinfo, and productinfo is a thin data class that stores some basic information about the small class.
Now let's look back at some of the Code marked by simplepager in category. aspx.
<% # Databinder. eval (container. dataitem, "ID") %>
From the knowledge of data binding, this ID is actually an attribute of productinfo.
If (itemsbyproduct. Count> 0)
Data Operations in items. aspx are basically the same as those in category. aspx.
The last code in items. aspx. CS is as follows:
Productname. Text = (iteminfo) itemsbyproduct [0]). productname;
Because itemsbyproduct is an arraylist, itemsbyproduct [0] actually returns an iteminfo.

From the above analysis, we can see that the benefits of business logic and business pragmatism are separated.

Author: Mike

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.