在DDD中定义多对多关系

时间:2020-12-08 04:02:30

Are many-to-many table structures defined as Value Objects in DDD? What if my many-to-many structure has a unique id? Also, what about 1-to-many relationships? For instance, if i have 2 structures Post & Comment with 1-to-many (respectively) wouldn't Comment be a Value Object since it technically cannot exist without a corresponding Post? But what if it has a unique Id (Comment_Id)? That makes it identifiable and therefore a non-value object entity? So, which is it?

在DDD中将多对多表结构定义为值对象吗?如果我的多对多结构具有唯一ID,该怎么办?还有,一对多关系怎么样?例如,如果我有2个结构Post&Comment with 1-to-many(分别)不会将Comment作为Value对象,因为如果没有相应的Post,它在技术上是不可能存在的?但如果它有唯一的ID(Comment_Id)怎么办?这使它可识别,因此是一个非价值对象实体?那么,这是什么?

Thanks!

1 个解决方案

#1


You are probably do not need DDD for blog. DDD is more about business logic. You will not have enough business logic to benefit from DDD.

您可能不需要DDD用于博客。 DDD更多的是关于业务逻辑。您将没有足够的业务逻辑来从DDD中受益。

If you still want to use DDD, not a problem. Some guidelines:

如果你仍然想使用DDD,不是问题。一些准则:

  1. If you want something to be referensable from outside of the business logic, it should be entity. So if you need reference to comments (otherwise you will not be able to manage them effectively) you need your comments to be an entities.
  2. 如果您希望从业务逻辑之外引用某些内容,那么它应该是实体。因此,如果您需要参考评论(否则您将无法有效地管理它们),您需要将您的评论作为实体。

  3. You have to maximize persistence ignorance. This means that could not expose identifiers to your business logic.
  4. 你必须最大化持久性无知。这意味着无法将标识符公开给您的业务逻辑。

  5. Many-to-many and one-to-many relationships are modeled with plain collections. The ORM tool should map this collections properly.
  6. 使用普通集合对多对多和一对多关系进行建模。 ORM工具应正确映射此集合。

Hope this helps to avoid DDD in your situation.

希望这有助于在您的情况下避免DDD。

#1


You are probably do not need DDD for blog. DDD is more about business logic. You will not have enough business logic to benefit from DDD.

您可能不需要DDD用于博客。 DDD更多的是关于业务逻辑。您将没有足够的业务逻辑来从DDD中受益。

If you still want to use DDD, not a problem. Some guidelines:

如果你仍然想使用DDD,不是问题。一些准则:

  1. If you want something to be referensable from outside of the business logic, it should be entity. So if you need reference to comments (otherwise you will not be able to manage them effectively) you need your comments to be an entities.
  2. 如果您希望从业务逻辑之外引用某些内容,那么它应该是实体。因此,如果您需要参考评论(否则您将无法有效地管理它们),您需要将您的评论作为实体。

  3. You have to maximize persistence ignorance. This means that could not expose identifiers to your business logic.
  4. 你必须最大化持久性无知。这意味着无法将标识符公开给您的业务逻辑。

  5. Many-to-many and one-to-many relationships are modeled with plain collections. The ORM tool should map this collections properly.
  6. 使用普通集合对多对多和一对多关系进行建模。 ORM工具应正确映射此集合。

Hope this helps to avoid DDD in your situation.

希望这有助于在您的情况下避免DDD。