我可以使用什么架构来处理购物车,其中每个产品都需要保存不同的属性

Mar*_*ing 4 architecture design-patterns

我正在构建一个与购物车非常相似的应用程序.用户从列表中选择产品,然后根据该产品,需要设置和保存一些属性.

例.

如果用户选择允许自定义颜色匹配的涂料类型,那么我必须允许他们输入通过颜色匹配过程接收的公式编号.所以我有一个Product Detail项,它是一个Paint类型的产品,而且sku的属性为"AllowCustomColorMatch",但我还需要在某个地方存储公式编号.

我不知道如何在我的代码中优雅地处理这个问题.我应该创建子类或产品吗?现在我正在保存用户在OrderDetails对象中输入的数据,该对象具有与其关联的Product的引用.

小智 5

您可以拥有一个包含产品属性集合的Product类

    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>();
        }

    }
Run Code Online (Sandbox Code Playgroud)

在数据源中,您可以拥有一个表,用于定义每种产品类型的属性.然后,当您渲染页面时,您知道要显示的属性以及要在字典中给出的名称.