Does everything have to be an aggregate? Many-to-Many Link

一笑奈何 提交于 2020-06-01 05:50:15

问题


Say I have two entities Notifications and Users.

I want to mark that a user has seen a specific notification.

This would commonly be done with a many-to-many relationship e.g. UserNotification

Because there is no invariant around this relationship (we don't care if "all" users have seen the notification) these users shouldn't be on the notification aggregate. On the opposite side, the users aggregate doesn't need a list of notifications on it

So that leads to say that the UserNotification (this relationship) is an aggregate of its own. However, because we are never going to reference this thing by Id, does it really really belong as one? It seems like just adding an aggregate for storing the data.

What should I do here?

  1. Just make an aggregate anyway and ignore the id?
  2. Put these notifications on the user or users on notifications. (does it belong on either, and would putting it on one not add weight and cause concurrency issues?)
  3. just make a crud table?
  4. An aggregate without the id and keep the composite key (is that allowed?)

thanks


回答1:


Does a Notification have its own lifecycle? Can a Notification exist without a User to be notified?

I could imagine a Notification to simply be a Value Object that gets copied to each affected User.




回答2:


have you considered modeling User and Notification as aggregates but NOT modelling the association at all?

There is a high probability of not needing to. The only usecase I can come up with is retrieving all notifcations of a user. this can be exposed in an repository interface via getNotifications(user: User): Iterable[Notifications] (scala syntax).

on the write side the saveNotification(notification: Notification, users: List[User]) could save the aggregate as well as populate the n:m table.


EDIT: on afterthought to this - my solution would introduce a source code dependency from notifications to users (at least on the repository) and your intiuition might be right - the notification should not know about the user at all.

But there has to be at least the concept of an Recipient which may perfectly reside in the notification "module" or "package". maybe you are crossing bounded contexts here and the User entity on one side should be translated to an Recipient value object on the other via Anti Corruption Layer.

It's up to you and your domain to decide. In this example it would perfectly make sense that the notification package has some knowledge about a "User". otherwise - what would be notified?



来源:https://stackoverflow.com/questions/61950447/does-everything-have-to-be-an-aggregate-many-to-many-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!