简体   繁体   English

DDD中的实体继承

[英]Entity Inheritance in DDD

I am trying to design a Domain Model of an E-Commerce system using DDD guidelines. 我正在尝试使用DDD指南设计电子商务系统的域模型。 I have a situation where I have 2 different types of Products. 我有两种不同类型的产品的情况。 One product is "Item" and the other product is "Package" (Group/Bundle of Items). 一种产品是“商品”,另一种产品是“包装”(商品组/捆绑商品)。 My domain model so far looks like this 到目前为止,我的域模型看起来像这样

public abstract class Product : IAggregateRoot
{
    public string ProductId { get; protected set; }

    // Many to Many relationship between Item and Package
    protected List<ItemPackageRelationship> ItemPackages => new List<ItemPackageRelationship>();

    protected List<Image> Images => new List<Image>();

    // Other properties shared between Item and Package
}

public class Item : Product
{
    // All properties and methods specific to Item

    public void SetImages(List<string> images)
    {
       // Set images for Item has its own business logic

       // Example: number of allowed images, size of image etc
    }
}

public class Package : Product
{
   // All properties and method related to package

    public void SetImages(List<string> images)
    {
       // Set images for Package has its own business logic

       // Exampe: number of allowed images, size of image etc
    }
}

All the behavior is defined inside Item and Package class and Product class just has shared properties. 所有行为都在Item和Package类内定义,而Product类仅具有共享属性。 Initially I was thinking to create 2 complete separate Aggregate roots (Item and Package) and no Product entity. 最初,我想创建2个完整的单独的聚合根(项目和包装),而没有产品实体。 The only reason I need Product is because of the Unique ProductId in the system. 我需要Product的唯一原因是因为系统中的Unique ProductId。 And all the relationship with other entities (like Images) needs to based on ProductId. 与其他实体(例如Images)的所有关系都需要基于ProductId。

I am not 100% confident what I am designing is correct and need some input here. 我不是100%确信我的设计正确无误,因此需要在此处提供一些输入。 Creating a Base entity just to share some properties is against any principle of DDD? 创建仅共享某些属性的基本实体是否违反DDD的任何原则? Is there anything else you guys would suggest to improve the design of my Domain Model? 你们还有其他建议来改进我的域模型的设计吗?

Thanks very much in advance. 首先十分感谢。

I think inheritence of entities should be driven by behavior, not by properties. 我认为实体的继承应该由行为驱动,而不是由属性驱动。 In other words you should look at other busines code, not at entities itself. 换句话说,您应该查看其他业务代码,而不是实体本身。 If you see relatively many places which should operate with an Item and with a Package in the same manner when iheritence can be used (but is not strictly necessary). 如果您看到可以使用继承性(但并非绝对必要)的地方,应该以相同的方式对ItemPackage进行操作的地方相对较多。 Inheritence is not reasonable if the single reason is sharing an unique identifier. 如果单个原因是共享唯一标识符,则继承是不合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM