Creating a Shopping Cart
I also struggled with the issue of how to represent customer shopping carts. The obvious choice here is the ASP.NET Profile object. Using the Profile object offers several advantages.
Browser cookies, Session state, and the Profile object are discussed in Chapter 22, "Maintaining Application State."
First, the Profile object is persistent. A customer can add items to a shopping cart during one visit and return many months later to complete a purchase.
Second, the Profile object is designed to handle both anonymous and authenticated users. Requiring a customer to register before adding items to a shopping cart does not create a good customer experience. An e-commerce application should make the process of adding an item to a shopping cart as easy as possible.
Finally, when you take advantage of the Profile object, you don't need to write any database logic to store shopping cart information. The Framework does all the hard work for you.
This all sounds good. Unfortunately, I
For example, when a customer views his shopping cart, the shopping cart should not display the price of a product when the customer added the item to their shopping cart. Instead, the shopping cart should display the current price of the product (imagine that a customer adds an item to the shopping cart while the item is on sale and returns many months later).
When you store items with the Profile object, all the items are stored as a blob. You can't perform database queries against the individual items contained in a Profile . In particular, you can perform database joins between items in a Profile and other database tables.
Therefore, I created a custom ShoppingCart component to represent customer shopping carts. The ShoppingCart component is in the App_Code folder.
The custom ShoppingCart component persists customer shopping carts. The component stores shopping carts in a database table named ShoppingCart s.
The custom ShoppingCart component handles both anonymous and authenticated users. It does it the same way that the Profile object does. Anonymous Identification is enabled in the web configuration file. When Anonymous Identification is enabled, a persistent cookie containing a unique identifier is added to each anonymous customer's browser. This unique identifier is used when an anonymous customer's shopping cart is stored and retrieved.
file includes a
event handler. This event handler calls a method of the
when an anonymous
Finally, the custom ShoppingCart component caches customer shopping carts in Sessions state. A customer's shopping cart does not need to be retrieved from the database with each page request. Instead, the shopping cart is stored in the web server's memory while the customer browses the website. This is done to improve performance.
Protecting Credit Card