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

Tuesday 21 April 2009

Do you hire IT personnel? Be part of a podcast!

We’re looking for Seattle-area hiring managers to share their thoughts on Microsoft Certification and its relevance in making hiring decisions. Tell us what you look for in prospective IT hires, and how certification influences your decision.

Some questions to consider: What does it tell you about a potential new hire when you see certifications on a resume? What advice would you give a job-seeker who is looking for a new position or considering a career change? In this difficult economy, your words of wisdom could make all the difference for someone seeking to get hired in the IT field.

Recording will take place at the Microsoft Learning offices in Bellevue, Washington, and will take only an hour or two of your time. Contact Monica Kilwine (v-monick@microsoft.com) for a pre-interview. Only Seattle-area candidates will be considered.

Calling Seattle-area MCTs: We need your voice for a Podcast

If you're a Microsoft Certified Trainer (MCT), here's your chance to be heard! We're creating a podcast about Microsoft Certification, and we'd like to include your stories.

As MCTs, your experience and expertise help your students become technically proficient on Microsoft technologies. Now we invite you to lend that same expertise to the subject of Microsoft Certification. Some questions to consider: How can certification help launch a career? How do you prepare for exams, and which certifications are most important? What tips can you share?

Your knowledge, and the respect you’ve earned as an MCT, will make this podcast highly valuable to listeners. Recording will take place at the Microsoft Learning offices in Bellevue, Washington, and will take only an hour or two of your time. Contact Monica Kilwine (v-monick@microsoft.com) for a pre-interview. Only Seattle-area candidates will be considered.

Microsoft Certified Professionals: Help us by sharing your resume

We're creating a new guide for hiring managers, and we need your help! The guide will include job descriptions and sample resumes, and your input will help ensure that the guide is relevant. The document will focus on these job roles:

· Server Administrator

· Database Administrator

· Database Developer

· Desktop Technician

· Systems Engineer

In order to make this guide as realistic as possible, and to highlight the skills, training and certification needed for candidates to be successful in these positions, we need to see sample resumes! If you're qualified for one of the jobs listed above, send your resume to Monica Kilwine (v-monick@microsoft.com). We'll be sure to remove any identifying information to protect your privacy. Your job experience is critical to making this guide a valuable resource for hiring managers, so please share your resume with us as soon as you can.

Hiring Managers: We need your advice!

We're creating a new guide for hiring managers, and we need your help! We're reaching out to Microsoft Certified Professionals (MCPs) to gather job descriptions and resumes for the following job roles:

· Server Administrator

· Database Administrator

· Database Developer

· Desktop Technician

· Systems Engineer

Where do you come in? If you're a hiring manager who works, hires or trains employees in these jobs, we need your feedback to help determine the accuracy of the job descriptions we gather. If any of the jobs listed above sound like yours or those of your direct reports, we invite you to join us in a conference call 9-10 AM, Pacific Time, Wednesday April 22nd to offer your advice. Contact Monica Kilwine (v-monick@microsoft.com) to take part in the call. Your input is vital to the creation of an effective hiring guide, and we look forward to your participation!