What architecture can I use to handle a shopping cart where each product requries different attributes to be saved

architecture, design-patterns

Solution

You can have a Product class with a collection of product properties

    public class Product
    {
        private Dictionary<string, string> properties;

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the price.
        /// </summary>
        /// <value>The price.</value>
        public double Price
        {
            get;
            set;
        }

        public Dictionary<string, string> Properties
        {
            get;
        }

        public Product()
        {
            properties = new Dictionary<string, string>();
        }

    }

In the datasource you can have a table that defines the properties to each type of product. Then when you render the page you know what properties to show and the name to give in the dictionary.

Problem

I am building an application that is very similar to a shopping cart. The user selects a product from a list, and then based on that product, a few properties need to be set and saved. Example. If the user selects a type of paint that allows custom color matches, then I must allow them to enter in a formula number that they received through a color match process. So I have an Order Detail item for a Product that is a type of Paint, and that sku has the attribute of "AllowsCustomColorMatch", but I need to store the Formula Number somewhere also. I'm not sure how to handle this elegantly throughout my code. Should I be creating subclasses or products? Right now I'm saving the data the user enters in an OrderDetails object that has a reference to the Product it is associated with.

Original source