locked
Shopping Cart Class Design RRS feed

  • Question

  • User269881539 posted

    I work on a large b2c ecomm site and need to upgrade / improve the shopping cart that I have previously built as we are about to offer a whole new range of delivery services and I want to take the chance to package it all up the best way possible, and make my life easier in the future by coding it in such a way that I can just access properties and methods of a class to determine whats going on inside each customers cart.

    Current cart is bespoke, uses SQL table for storage and cookie for state management - this will not change I think as it works well and allows us to see what is going on, abandonments etc. I have a basic Cart class that handles adding / removing and updating the cart contents. The actual cart page just uses a repeater bound to the results of a stored procedure to output the contents, and subsequent checkout pages where I need to reexamine the cart contents do similar.

    What I'd like to do is beef up the cart class so that I can just call a "get" or "populate" method and then loop through the contents and show on screen - probably still in a repeater as I like to have control over the output.

    Also then because the class will effectively hold the contents of the cart, and know details about each and every item I can immediately and easily detect if for example all items are currently in stock, and react accordingly. Also I could access one property that gives me the total, or tells me if the products have a mixed availability - that sort of thing. Essentially I will flesh it out so that the class can tell me everything about the contents of someones cart in a simplistic way - sometimes at an item level, sometimes at a summary level.

    I think the bit I am not sure about is the best way to hold, and then iterate through the actual items within the cart? Should I create a cart item class and then create a collection of items within the main cart class? I've also seen some suggestions about returning data tables etc? I've not done that within a class before. One thing that may dictate this, is that I need to be able to sort the items (most recently added first) although I do that at the database level at the moment anyway.

    I want to do this properly as I have masses of work to completely overhaul cart / checkout areas of the site and want to give myself the best possible start.

    Many Thanks

    Chris

     

    Friday, May 14, 2010 7:26 AM

Answers

  • User-952121411 posted

    So I am thinking of creating a class clsBasketItem
     

    One word of advice; drop the Hungarian notation or 'cls' in front of the class name.  You can decipher the different object types in VS.NET fine without the prefix.  No worries though, I see that a lot especially from old VB6 developers.  In fact take the following quote from the MSDN article below: http://msdn.microsoft.com/en-us/ms229045.aspx

    "Do not use Hungarian notation."

    So I am thinking of creating a class clsBasketItem with properties pertaining to each item (code, description, qty etc etc), and then in my main clsBasket class fetching the cart contents from my SQL table into a collection of the clsBasketItem class - does that sound right?

    Well there are at least 100 different ways you could design the class.  You could have an 'Items' class that reflects the items in the database that is displayed for the client.  Then upon ordering 1..n items you could get each of the items data and have a property on your 'Basket' or maybe more appropriately named 'Order' class of:  List(of Items). 

    And the LINQ to objects will help me make specific queries against my object as required?

    Yes as you become familiar with LINQ you will be able to query your objects if needed to produce a subset of results via a query directly into your class types, or into anonymous types as well.  There is a lot of power with LINQ.

    At this point I recommend getting your classes built and organized 1st to properly reflect your relational data and then begin the development for the interaction between the layers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 18, 2010 3:11 PM

All replies

  • User-952121411 posted

    You need to work with objects and not ADO.NET objects if possible in your presentation layer, and specifically generic lists of objects.  A generic list implements the IEnumerable interface required for binding to all sorts of controls: Repeater, GridView, DDL, etc.   Then once you are working with objects and lists of objects you can use LINQ to Objects to query into or on a list to find out all of the information you need.  The key here is to design your classes well so that all of the information needed will be available via the objects and then leverage a technology like LINQ to query the data.

    Start by reading the following links for some more information that will help give you the power to access, iterate, and manipulate your data as objects:

    .NET Object Collections Using Generics 101: 

    http://allen-conway-dotnet.blogspot.com/2009/11/net-object-collections-using-generics.html

    LINQ to Objects:

    http://msdn.microsoft.com/en-us/library/bb397919.aspx

     

    Friday, May 14, 2010 1:35 PM
  • User269881539 posted

    Thanks for that - I have created classes using generic collections (List(Of myClass)) before actually but not used them so much to iterate through etc - but knowing I can, or at least that I should be able to is great. Normally I have used this setup to return sub records of a main class and perhaps bind to a drop down list for example (i.e. show me all sizes related to a product and display in a drop down)/

    So I am thinking of creating a class clsBasketItem with properties pertaining to each item (code, description, qty etc etc), and then in my main clsBasket class fetching the cart contents from my SQL table into a collection of the clsBasketItem class - does that sound right?

    And the LINQ to objects will help me make specific queries against my object as required?

    Tuesday, May 18, 2010 11:13 AM
  • User-952121411 posted

    So I am thinking of creating a class clsBasketItem
     

    One word of advice; drop the Hungarian notation or 'cls' in front of the class name.  You can decipher the different object types in VS.NET fine without the prefix.  No worries though, I see that a lot especially from old VB6 developers.  In fact take the following quote from the MSDN article below: http://msdn.microsoft.com/en-us/ms229045.aspx

    "Do not use Hungarian notation."

    So I am thinking of creating a class clsBasketItem with properties pertaining to each item (code, description, qty etc etc), and then in my main clsBasket class fetching the cart contents from my SQL table into a collection of the clsBasketItem class - does that sound right?

    Well there are at least 100 different ways you could design the class.  You could have an 'Items' class that reflects the items in the database that is displayed for the client.  Then upon ordering 1..n items you could get each of the items data and have a property on your 'Basket' or maybe more appropriately named 'Order' class of:  List(of Items). 

    And the LINQ to objects will help me make specific queries against my object as required?

    Yes as you become familiar with LINQ you will be able to query your objects if needed to produce a subset of results via a query directly into your class types, or into anonymous types as well.  There is a lot of power with LINQ.

    At this point I recommend getting your classes built and organized 1st to properly reflect your relational data and then begin the development for the interaction between the layers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 18, 2010 3:11 PM
  • User269881539 posted

    OK I have the basics of the setup - I have the overall class which also contains a collection of the class that represents the items. I have also been able to bind this to my repeater and all is well so far.

    Now though I need to "query" my collection - is this where I really need to look at LINQ?

    For example I may have say 5 items in my basket (collection) - I would like to know if between them 1 or more has a certain value in a certain column - e.g. one of my columns is an age rating, so I'd like to know if the basket as a whole requires confirmation that the customer is over 18 (we sell sharp stuff!) - so if 1 or more items has an appropriate value in this column, the basket as a whole can be deemed to need authorisation.

    I know I could iterate across all items but I have 2 or 3 similar checks to do and I don't want to repeat the process. Ideally I'd like to have a property (function?) on my main basket class that just returns AgeWarning = True (or false of course!)

    Would the BinarySearch or Find type methods be soemthing I should look at? They seem more geared to obtaining specific rows within the collection though?

    Re the hungarian notation - borne out of my VB / VBA days - 18 years served man and boy!

    Thanks.

    Thursday, May 20, 2010 9:06 AM
  • User-952121411 posted

    I would like to know if between them 1 or more has a certain value in a certain column - e.g. one of my columns is an age rating, so I'd like to know if the basket as a whole requires confirmation that the customer is over 18
     

    You could filter your collection using a LINQ query based on 'WHERE MyType.Age > 18'.  The resulting query would return only the objects that met the criteria of your query.  This will become more apparent as to how it works as you familiarize yourself a bit more with LINQ.

    I know I could iterate across all items but I have 2 or 3 similar checks to do and I don't want to repeat the process. Ideally I'd like to have a property (function?) on my main basket class that just returns AgeWarning = True (or false of course!)

    Upon obtaining the results from the query above you could work with just the collection above and return it from a method for example if needed.  Or you could take it a step further and update the property 'AgeWarning' on the types that met the criteria directly in the query.  That would take care of both requirements at the same time.  I recommend working through some basic LINQ query examples in the following link:

    Introduction to LINQ in Visual Basic:

    http://msdn.microsoft.com/en-us/library/bb763068.aspx

    Friday, May 21, 2010 9:03 AM