设置继承以避免代码中的未来冗余的最佳方法是什么?

时间:2012-09-16 00:18:10

标签: c# inheritance

我有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; }
}

设置继承的最佳方式是什么,以便车辆,零件,用品和任何未来的销售项目类别[例如。衣服]可以添加没有太多的忙乱与冗余属性编码?

3 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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

设计您现在所拥有的,但要足够灵活,以便在需要时添加新属性。例如,现在我不会添加qrcode图像,但稍后可能会增加它。

您的问题实际上是针对课堂设计还是最终用于数据库设计?

答案 1 :(得分:1)

我决定将Seasonal&amp;停止使用产品并让产品继承自实体。 非常感谢评论/言论警察。我没有意识到我们在这个网站上有更多的编辑而不是回答帮助者。所以,这就是我要继续下去的方式:

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

答案 2 :(得分:0)

随着应用程序的增长,有一段时间“有一种关系”(组合)比“是一种关系”(继承)更容易维护。看起来这是其中一种情况。您可以对其进行设置,以便每个实体都有一个产品类。您可以使用控制反转将产品注入这些实体。

此链接解释了本文的基本原理。

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

相关问题