2

我有 7 节课:

public class Entity
{
   public int Id { get; set; }     
}

public class Product : ????
{
    // Contructor
    public Product()
    {
        Photos = new HashSet<PhotoSource>();
        ProductFeatures = new HashSet<ProductFeature>();
    }

    // Primitives
    public string ProductName { get; set; }
    public string InternalSKU { get; set; }
    public string ModelNumber { get; set; } 
    public string Description { get; set; }
    public int QtyPerUnit { get; set; }
    public double UnitPrice { get; set; }
    public int UnitsInStock { get; set; }
    public int UnitsOnOrder { get; set; }
    public int? ReOrderLevel { get; set; }
    public string Warranty { get; set; }

    // Foreign Keys
    public int SubCategoryID { get; set; }
    public int VendorId { get; set; }

    // Navigation Properties
    // Classes
    [ForeignKey("SubCategoryID")]
    public virtual SubCategory SubCategory { get; set; }
    [ForeignKey("VendorId")]
    public virtual Vendor Vendor { get; set; }

    // Collections
    public virtual ICollection<PhotoSource> Photos { get; set; }
    public virtual ICollection<ProductFeature> ProductFeatures { get; set; }
}


public class ProductSeasonal : ????
{
    // Primitives
    public int? OffSeasonDiscount { get; set; }
    public DateTime SeasonStartDate { get; set; }
    public DateTime SeasonEndDate { get; set; }
    public int? QtyLimitedTo { get; set; }
}

public class ProductDiscontinued : ????
{
    // Primitives
    public DateTime DiscontinuedDate { get; set; }
    public int DiscontinuedDisount { get; set; }
}

public class Supply : ????
{
    // Primitives
    public String UnitMeasurement { get; set; }
}

public class Part : ????
{
    // Primitives
    public String UnitMeasurement { get; set; }
}

 public class Vehicle : ????
{
    // Constructor
    public Vehicle()
    {
        ExteriorFeatures = new HashSet<ProductFeature>();
        InteriorFeatures = new HashSet<ProductFeature>();
        SafetyFeatures = new HashSet<ProductFeature>();
    }

    // Primitives
    public string VIN { get; set; }
    public int Year { get; set; }
    public int CylinderSize { get; set; }
    public double EngineSize { get; set; }
    public string StyleType { get; set; } //Truck, SUV, Sedan, Convertible, etc
    public string TransmissionType { get; set; }
    public string InteriorColor { get; set; } 
    public string ExteriorColor { get; set; }

    // Foreign Keys
    public virtual int MakeId { get; set; }


    // Navigation Properties
    // Classes
    [ForeignKey("MakeId")]
    public virtual VehicleMake Make { get; set; }

    // Collections
    public virtual ICollection<ProductFeature> InteriorFeatures { get; set; }
    public virtual ICollection<ProductFeature> ExteriorFeatures { get; set; }
    public virtual ICollection<ProductFeature> SafetyFeatures { get; set; }
}

什么是设置继承的最佳方式,以便车辆、零件、用品和任何未来的销售项目类 [例如。服装] 可以毫不费力地添加多余的属性吗?

4

3 回答 3

1

最好的开始方法是写下当前感兴趣的每个项目的所有属性。

因此,任何产品都会有价格和一些唯一的 id(sku 编号),可能还有条形码和图像。

所以,这些可能是一些父类的开始。

当您浏览其他产品时,您可能会发现共性。

如果您需要开始销售牛仔裤,请查看可能需要哪些其他服装,因为您可能希望列出材料或款式。

但是,不要试图设计你的类,以便它们能够处理任何事情。

为您现在拥有的东西进行设计,但使其足够灵活,以便您可以在需要时添加新属性。例如,现在我不会添加 qrcode 图像,但以后可能会很常见,可以添加。

您的问题实际上是针对类设计还是最终针对数据库设计?

于 2012-09-16T00:42:11.650 回答
1

我决定将季节性和停产合并到产品中,并让产品从实体继承。非常感谢评论/文字警察。我没有意识到我们在这个网站上的编辑比回答助手还多。所以,这就是我将如何进行:

     public Product()
    {
        OrderDetails = new HashSet<OrderDetail>();
        Photos = new HashSet<PhotoSource>();
        ProductFeatures = new HashSet<ProductFeature>();
    }

    // Primitives
    public string ProductName { get; set; }
    public string InternalSKU { get; set; }
    public string ModelNumber { get; set; } 
    public string Description { get; set; }
    public int QtyPerUnit { get; set; }
    public double UnitPrice { get; set; }
    public int UnitsInStock { get; set; }
    public int UnitsOnOrder { get; set; }
    public int? ReOrderLevel { get; set; }
    public string Warranty { get; set; }
    // Primitives for Disontinues
    public DateTime? DiscontinuedDate { get; set; }
    public int? DiscontinuedDisount { get; set; }
    // Primitives for Seasonal
    public int? OffSeasonDiscount { get; set; }
    public DateTime? SeasonStartDate { get; set; }
    public DateTime? SeasonEndDate { get; set; }
    public int? QtyLimitedTo { get; set; }


    // Foreign Keys
    public int SubCategoryID { get; set; }
    public int VendorId { get; set; }

    // Navigation Properties
    // Classes
    [ForeignKey("SubCategoryID")]
    public virtual SubCategory SubCategory { get; set; }
    [ForeignKey("VendorId")]
    public virtual Vendor Vendor { get; set; }

    // Collections
    public ICollection<OrderDetail> OrderDetails { get; set; }
    public virtual ICollection<PhotoSource> Photos { get; set; }
    public virtual ICollection<ProductFeature> ProductFeatures { get; set; }
}
于 2012-09-16T00:45:01.207 回答
0

Some times "has a relationship" (composition) are easier to maintain than "is a relationship"(inheritance) as the application grows. Looks like this is one of those scenarios. You can set it up so that each of the entities has a product class. And you can use inversion of control to inject product into these entities.

This link explains the rationale in this article.

http://www.artima.com/designtechniques/compoinh4.html

于 2012-09-16T07:40:30.230 回答