Monday, 27 April 2009

A sample shopping cart

Lately I was helping someone on one of the forums and they were having a lot of problems with it. So in my craziness I decided it would be a good idea to make a sample solution and then use that as a nifty blog post.

So here we go.

This project was coded against the 2.0 Framework as it was required by the host. Same concepts still apply with 3.5 just the code might be a bit shorter (and I will upload a new post explaining the differences for that one)

Before I start I used some code for the database wrapper from here and then modified it a bit for my own purposes.

First off I created a project called Common that would hold all my classes that would be used by the solution.

I designed a class that represents my product. Its a very simple class with four public properties.
ProductClass

I then added another class file that represents the items in the cart.
CartItemClass

It inherits from the product object. The only additional property is the Amount.

On to the DataAccess project. This project handles all the interaction with the database.
The ProductDal class has 2 methods. The first one gets a list of products from the database.
DataAccessGetAll

The second one gets a particular item from the database.
DataAccessGetItem

I also have a helper class that contains two generic methods that allows me to populate an object with data or generate a list of specified objects.

CreateObject
CreateList.jog

The CreateObject method uses Reflection to populate the objects from the DataRow. The trick is to use the same names for the columns in the database as you use in the object. This makes a very handy and quick way of populating your objects from the database without using any particular ORM tool such as nHibernate

The Business project would normally contain the business logic required for this project but in this case, it is basically a pass through to the DataAccess project.
Business

The Web site project contains two pages, Catalog.aspx and ShoppingCart.aspx

Both are fairly similar in how they approach what they need to do.

The Catalog page uses a Repeater server control to show the product list.

CatalogRepeater

The main thing to notice here is the button. It has two parts, one is the CommandArgument that has the Id of the Product in it and the CommandName which is set to AddToCart. This could be any piece of text as long as you can trap it.

In the Catalog.aspx.cs file I create first a new event handler that handles the repeater item commands such as the button click

EventHandlerAdd

In the Page load I check if its a PostBack so that I only bind if its not a postback. Additionally I set the label so that you can see if the cart is working.

Now the event handler for the Repeater item command. This is the part that adds items to the shopping cart.

What I do first is check what type of command I am getting, ie AddToCart. If you have a couple of different buttons or linkbuttons or whatever you could use that check to find what code to run.

Now the next line creates a new list of CartItem and assigns the value either to a new list or the list in my session. The syntax that it uses is just shorthand.
AssignCartList

So now I have a list of objects of type CartItem.

I create a new CartItem object by using the function called GetItem and pass in the id of the product I want. This Id is the CommandArgument that i set in my button.


CreateCartItem

Now the next bit is to make sure we only have one product of each type in the shopping cart and just update the amount if its there.

So I create a new CartItem called hasitem and try to find it in my list. If its there, it will come back as the same object otherwise it will be null.
CheckIfItemInCart

Next I check to see if my hasitem is null and if its not, I add 1 to the amount and then I remove it from my list so that I only add the object once. You will see why in a minute. If the hasitem is null, I just use my new CartItem above and set amount to 1.

After all that, I have a CartItem that has the correct amount and add that to the list. This makes sure that there is unique Products in the shopping cart
CheckItem

I then put the list back into the session (or for the first time it doesn’t matter) and then I set the label to the count size of the list, which isn’t the actual number of items but just the number of items in the list e.g.. if you have 2 products with amounts of 2, it wont show 4, it will only show that you have 2 items because its using the count on the list.


So that is the catalog page explained onto the ShoppingCart.aspx file.

Again I am using a Repeater to show the data and using a similar technique to handle the events.
CatalogRepeater

So following the same ideas in the Catalog.aspx we have an event handler to handle the repeater item commands. We bind the repeater to the list of CartItem we have in our session. We have to check any time before using the session object to see if its there otherwise you will get Null reference exceptions.

Since we have a managed list we don’t need the CommandArgument in the button. But if we had sorting etc, then you would need it.

So we just remove the item at the same index of the RepeaterItem
CartItemEvent

And that’s a very simple shopping cart that uses generics and some reflection to do the job.

This solution was created in VS2008 and targetted for .NET 2.0. The script to create the database is in the root of the solution file. Also if you are using VS 2005 you won't be able to open the solution file, but you should be able to create a blank solution and add the projects manually

The full solution can be download here

No comments: