设置继承以避免将来代码中出现冗余的最佳方法是什么
本文关键字:冗余 最佳 是什么 方法 继承 代码 将来 设置 | 更新日期: 2025-05-08 13:43:52
我有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; }
}
建立遗产的最佳方式是什么?这样,车辆、零件、用品和任何未来销售的物品类别(如服装)都可以添加,而不会对多余的财产进行编码?
最好的开始方式是记下当前感兴趣的每个项目的所有属性。
因此,任何产品都会有价格和一些唯一的id(sku编号),也许还有条形码和图像。
因此,这可能是某个父类的开始。
当你浏览其他产品时,你可能会发现共性。
如果你需要开始销售牛仔裤,那么看看还需要什么其他衣服,因为你可能想列出材料或款式。
但是,不要试图设计你的类,让它们能够处理任何事情。
为您现在拥有的内容进行设计,但要使其足够灵活,以便在需要时添加新属性。例如,现在我不会添加qrcode图像,但稍后可能会添加它。
您的问题实际上是针对类设计,还是最终针对数据库设计?
我决定将Seasonal&中止为产品并使产品从实体继承。非常感谢评论/文字警察。我没有意识到我们网站上的编辑比答案助手还多。所以,这就是我将如何进行:
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; }
}
随着应用程序的发展,有时"有关系"(组合)比"是关系"(继承)更容易维护。看起来这就是其中一个场景。您可以将其设置为每个实体都有一个产品类。您可以使用控制反转将产品注入到这些实体中。
此链接解释了本文中的基本原理。
http://www.artima.com/designtechniques/compoinh4.html