域建模帮助:产品& ProductType& ProductyTypeProperties& ProductyTypePropertyValues

时间:2011-07-01 19:06:59

标签: c# design-patterns data-structures domain-driven-design data-modeling

我有一个问题域,其中用户应该能够创建“ProductType”对象,其中每个ProductType对象应该具有聚合的“ProductTypeProperties”列表,并且每个ProductTypeProperty对象应该具有聚合的“ProductTypePropertyValues”列表。

用户能够创建“Product”对象并将少量ProductTypes与之关联。

当用户将Product与少量ProductTypes相关联时,用户可以为Product对象指定ProductTypeProperties的值。

ProductTypeProperties可以具有属于不同选择模式的值,例如:“one-choose”,“multiple-choose”和“string / integer / decimal input”

我不确定如何设计这样的域对象模型。特别是如何在Product对象上应用ProductType的属性值。

我不介意此时的持久性,只关于对象域模型,因为我可以自由选择SQL / Document / Object / Graph数据库。

对象结构现在看起来像这样:

ProductType
    List<ProductTypeProperty>
        List<ProductTypePropertyValue>

Product
    List<ProductType>

我现在使用的C#类定义是:

public class Product {
    public string Name { get; set; }
    public List<ProductType> AssociatedProductTypes { get; set; }
    // how to apply ProductType's property values to a Product object?
}

public class ProductType {
    public string Name { get; set; }
    public List<ProductTypeProperty> AggregatedProperties { get; set; }
}

public class ProductTypeProperty {
    public string Name { get; set; }
    public List<ProductTypePropertyValue> AggregatedAvailableValues { get; set; }
}

public class ProductTypePropertyValue {
    public string Name { get; set; }
}

看起来试图在对象中应用类/对象结构,其中“ProductType”是“类”,“Product”是“对象”,可以是实例“ProductTypes”和“inherit”属性和可用值来自每种相关产品类型。

我从未做过像这样的对象模型,所以如何做到这一点非常有趣。 感谢您的任何想法和建议。

1 个答案:

答案 0 :(得分:2)

您可以按如下方式映射ProductTypeProperty:

通用基本属性类,其中TValue将确定属性值的类型(可以是字符串,十进制,整数或多选):

    public abstract class ProductTypePropertyBase<TValue>
    {
        public string Name { get; set; }
        public TValue Value { get; set; }

        protected ProductTypePropertyBase(string name, TValue value)
        {
            Name = name;
            Value = value;
        }
    }

为每种类型的属性创建一个嵌套类,例如,您可以创建一个简单的字符串属性:

public class ProductTypeStringProperty : ProductTypePropertyBase<string>
{
    public ProductTypeStringProperty(string name, string value) : base(name, value)
    {
    }
}

对于复杂的属性类型,您可以实现多种选择:

public class ProductTypeMultipleChoiceProperty : ProductTypePropertyBase<MultipleChoiceValue>
{
    public ProductTypeMultipleChoiceProperty(string name, MultipleChoiceValue value) : base(name, value)
    {
    }
}

其中MultipleChoiceValue是另一种表示字符串列表的类型。

相关问题