Preface
The shopping basket module is one of the heaviest modules in petshop4. In the database, the cart table is the most closely related to the shopping basket (for more information about the cart table, see the first part of this series of blog posts ).
Data access layer (DAL) of the shopping basket)
The entity class is designed in the shopping basket module to quickly assign values to fields in the database, reflecting the integrity of the data structure. All entity classes of petshop4 are saved in the project model.
The carditeminfo class basically corresponds to the card table, which enables quick access to the card table.
Data Access to the shopping basket is implemented through the profile factory model. Basic Principles of the factory ModelArticleYou only need to know how to call the profile. Finally, database access is implemented through the petshopprofileprovider class in the sqlprofiledal project. The following describes how to add a basket in this class:
/// Retrieve collection of Shopping Cart items
Public ilist <cartiteminfo> getcartitems (string username, string appname, bool isshoppingcart)
/// Update shopping cart for current user
Public void setcartitems (INT uniqueid, icollection <cartiteminfo> cartitems, bool isshoppingcart)
/// Update activity dates for current user and application
Public void updateactivitydates (string username, bool activityonly, string appname)
/// Retrive unique ID for current user
Public int getuniqueid (string username, bool isauthenticated, bool ignoreauthenticationtype, string appname)
/// Create profile record for current user
Public int createprofileforuser (string username, bool isauthenticated, string appname)
Flowchart for saving shopping basket information:
Business logic layer (BLL) of the shopping basket)
In the layer-3 structure, the business logic layer is located in the middle layer, which is a connecting layer. It provides an interface for users to call and starts the data access layer to complete user operations, because the Dal has nothing to do with the business logic, BLL is the key layer for implementing the logic processing of user data. when the SQL statements at the data access layer change, you only need to change part of the Dal layer without affecting the top layer of BLL. bll is mainly used to implement the operation functions of the current business.
The processing class of all business logic layers in the system is located under the BLL project. The business logic of the shopping basket function in pepshop4 mainly includes: Adding goods to the shopping basket and removing goods from the shopping basket, update the quantity of items in the shopping basket, clear the shopping basket, obtain the total number and price of items in the shopping basket, and obtain the records of items in the shopping basket. the card class structure is as follows:
UI of the shopping basket)
The interface layer is located under the WEB Project, and the interface layer file of the shopping basket is shoppingcard. aspx. Open the HTML view of the file,
First, the parent layout: masterpagefile = "~ /Masterpage. Master"
Second, a user control is registered on the page: shoppingcardcontrol, registrationCodeAs follows:
<% @ Register src = "controls/shoppingcartcontrol. ascx" tagname = "shoppingcartcontrol" tagprefix = "petshopcontrol" %>
The following figure shows the control on the page:
<Petshopcontrol: shoppingcartcontrol id = "shoppingcartcontrol1" runat = "server"> </petshopcontrol: shoppingcartcontrol>
Open the design interface of the shoppingcartcontrol. ascx control. The shopping basket item list is displayed by a reparter control. The record display format is as follows:
<Itemtemplate>
<Tr class = "listitem">
<TD>
<Asp: imagebutton id = "btndelete" runat = "server" borderstyle = "NONE" causesvalidation = "false"
Commandargument = '<% # eval ("Itemid") %>' commandname = "Del" imageurl = "~ /Comm_images/button-delete.gif"
Oncommand = "cartitem_command" tooltip = "delete"/>
</TD>
<TD style = "width: 100%;">
<A runat = "server" href = '<% # string. Format ("~ /Items. aspx? Itemid = {0} & productid = {1} & categoryid = {2} ", Eval (" Itemid "), Eval (" productid "), Eval (" categoryid ")) %> '> <% # string. format ("{0} {1}", Eval ("name"), Eval ("type") %> </a>
</TD>
<TD>
<Asp: textbox id = "txtquantity" runat = "server" columns = "3" text = '<% # eval ("quantity ") %> 'width = "20px"> </ASP: textbox>
</TD>
<TD align = "right"> <% # eval ("price", "{0: c}") %> </TD> <TD>
<Asp: imagebutton id = "btntowishlist" runat = "server" alternatetext = "move to wish list" causesvalidation = "false" commandargument = '<% # eval ("Itemid ") %> 'commandname = "move" imageurl = "~ /Comm_images/button-wishlist.gif "oncommand =" cartitem_command "tooltip =" move to wish list "/>
</TD>
</Tr>
</Itemtemplate>
Display shopping basket Interface
Note that when you open the shopping basket interface, the system automatically retrieves information about the shopping basket you have previously added and loads it to the interface. How is this function implemented?
The previous shopping information loading function is completed in the page_prerender event on the page. This event calls the "bindcart" method. The code for the "bindcart" method is as follows:
Private void bindcart (){
Icollection <cartiteminfo> cart = profile. shoppingcart. cartitems;
If (Cart. Count> 0 ){
Repshoppingcart. datasource = cart;
Repshoppingcart. databind ();
Printtotal ();
Plhtotal. Visible = true;
}
Else {
Repshoppingcart. Visible = false;
Plhtotal. Visible = false;
Lblmsg. Text = "your cart is empty .";
}
}
Add item to shopping basket
Protected void page_preinit (Object sender, eventargs e ){
If (! Ispostback ){
String Itemid = request. querystring ["additem"];
If (! String. isnullorempty (Itemid )){
Profile. shoppingcart. Add (Itemid );
Profile. Save ();
// Redirect to prevent duplictations in the cart if user hits "refresh"
Response. Redirect ("~ /Shoppingcart. aspx ", true );
}
}
}
The statement: profile. shoppingcart. Add (Itemid); is a key statement for function operations. The implementation process of this statement is as follows:
Call the add method in the cart class in The bll project → call the setpropertyvalues method of the petshopprofileprovider class in the profile project
→ Call the setcartitems method of the same type to add product information to the shopping basket
→ Call the createprofileforuser method of the petshopprofileprovider class in the sqlprofiledal project to add user configuration information
Change item quantity
Protected void btntotal_click (Object sender, system. Web. UI. imageclickeventargs e ){
Textbox txtquantity;
Imagebutton btndelete;
Int qty = 0;
Foreach (repeateritem row in repshoppingcart. Items ){
Txtquantity = (textbox) Row. findcontrol ("txtquantity ");
Btndelete = (imagebutton) Row. findcontrol ("btndelete ");
If (Int. tryparse (webutility. inputtext (txtquantity. Text, 10), Out qty )){
If (qty> 0)
Profile. shoppingcart. setquantity (btndelete. commandargument, qty );
Else if (qty = 0)
Profile. shoppingcart. Remove (btndelete. commandargument );
}
}
Profile. Save ();
Bindcart ();
}
Calculate the total commodity price
Private void printtotal (){
If (profile. shoppingcart. cartitems. Count> 0)
Ltltotal. Text = profile. shoppingcart. Total. tostring ("C ");
}
Remove items from the basket
Protected void cartitem_command (Object sender, commandeventargs e ){
Switch (E. commandname. tostring ()){
Case "Del ":
Profile. shoppingcart. Remove (E. commandargument. tostring ());
Break;
Case "move ":
Profile. shoppingcart. Remove (E. commandargument. tostring ());
Profile. wishlist. Add (E. commandargument. tostring ());
Break;
}
Profile. Save ();
Bindcart ();
}
Transfer from anonymous user to verified user Shopping Basket
An important feature of petshop4 is that it allows users to select products without logging on, and add products to the shopping basket. After a user logs on, save the item selected before logon to the verified personalized configuration.
. Net 3.5 provides an anonymous login method, which automatically generates a username for the user. When an anonymous user selects a commodity to the shopping basket, the system automatically saves the anonymous user name in the Profile Table and the user's shopping basket. After the user logs on, you only need to replace the username in the table.
To implement this function, first activate it in Web. config:
<Anonymousidentification enabled = "true"/>
<Profile automaticsaveenabled = "false" defaultprovider = "shoppingcartprovider">
<Providers>
<Add name = "shoppingcartprovider" connectionstringname = "sqlprofileconnstring" type = "petshop. profile. petshopprofileprovider" applicationname = ". Net Pet Shop 4.0"/>
<Add name = "wishlistprovider" connectionstringname = "sqlprofileconnstring" type = "petshop. profile. petshopprofileprovider" applicationname = ". Net Pet Shop 4.0"/>
<Add name = "accountinfoprovider" connectionstringname = "sqlprofileconnstring" type = "petshop. profile. petshopprofileprovider" applicationname = ". Net Pet Shop 4.0"/>
</Providers>
<Properties>
<Add name = "shoppingcart" type = "petshop. BLL. Cart" allowanonymous = "true" provider = "shoppingcartprovider"/>
<Add name = "wishlist" type = "petshop. BLL. Cart" allowanonymous = "true" provider = "wishlistprovider"/>
<Add name = "accountinfo" type = "petshop. model. addressinfo" allowanonymous = "false" provider = "accountinfoprovider"/>
</Properties>
</Profile>
The second is to correctly handle anonymous migration. The system uses the profile_migrateanonymous event for processing. This event is generally in the global. asax file.
Void profile_migrateanonymous (Object sender, profilemigrateeventargs e ){
Profilecommon anonprofile = profile. getprofile (E. anonymousid );
// Merge anonymous shopping cart items to the authenticated shopping cart items
Foreach (cartiteminfo cartitem in anonprofile. shoppingcart. cartitems)
Profile. shoppingcart. Add (cartitem );
// Merge anonymous wishlist items to the authenticated wishlist items
Foreach (cartiteminfo cartitem in anonprofile. wishlist. cartitems)
Profile. wishlist. Add (cartitem );
// Clean up anonymous profile
Profilemanager. deleteprofile (E. anonymousid );
Anonymousidentificationmodule. clearanonymousidentifier ();
// Save Profile
Profile. Save ();
}