21st
AUG

Design Approach for a reusable Shopping Cart Module

Posted by Sambeet Patra under Software Architecture, Web Services

Shopping Cart is a critical piece of an ecommerce website. It allows temporary storage of items selected till the user decides on placing the final order. In this article I will present a simple approach to design a shopping cart component that can be used in multiple web sites.

The primary goals of my approach are 

  • Re-usability: As described earlier, the cart should be reusable.
  • Extensibility: Each website can store custom data in the cart.

Design Considerations:

Shopping cart storage, by definition will hold product data for the user session till an order is placed. While a shopping cart can be conceived to have a definite schema, it is preferable to treat this design as a solution to maintain the shopping cart instead of mandating the structure of the data. Taking this into consideration, the shopping cart schema will be made flexible and the storage mechanism [for example the database] will be agnostic of the schema. In other words, we will consider serializing the shopping cart data and save it as a BLOB into the database.

One other fundamental requirements for the shopping cart solution is the ability to identify the cart for a user session. However, different sites may have different implementation to identify user session. Some sites may use cookies; the other sites may use server session ids. The shopping cart solution should be agnostic of the mechanism of user session identification.

Class Diagram:

The main components of the shopping cart application are depicted in the following diagram:

Shopping Cart Class Diagram

Shopping Cart Class Diagram

KeyValuePair: This is a simple structure to hold a key value pair. This structure will be used to maintain client specific data in the shopping cart.

Cart: Represents a Shopping Cart. The Cart class has an array of Cartitem classes to hold the cart line item details. It has some predefined fields to hold billing and shipping information. However, it will also have an array of KeyValuePairs to hold client specific data. For example, website 1 may be storing the user phone number whereas website 2 stores user region information. It will be difficult and practically impossible to identify all possible information that may be maintained by all the web sites using the shopping cart solution. The KeyValuePair array presents a simple option of storing additional attributes for the shopping cart without distorting the schema.

CartItem: Represents a line item in the cart. This also has an array of KeyValuePair to hold additional information.

CartManager: All classes explained so far form the overall schema of the cart. The CartManager implements and exposes functionalities to save / retrieve cart.

DB Design:

 The Cart class will be made serializable and the serialized data will be stored in the database. The advantage of serialized data is that we do not need to have a complex DB design and the design will not change when the cart schema changes. The DB design is presented below.

DB Schema

DB Schema

Note that the field CartData contains a serialized version of the Cart object.

Security Considerations:

The cart solution may require following functionalities

  • Ability to encrypt the persisted data. This is applicable if the cart contains sensitive information. This may be achieved by encrypting the serialized cart information before saving it to the database.
  • Client Authentication: If the solution is exposed as a web service, there may be requirement to restrict the usage of the cart solution only to specific web sites. This is achieved by using digital certificates to identify clients. Use of WS-Security specifications is recommended for use in web service.

Leave a Reply