Domain-driven project: how to model relationships that are large but have little behavior

Let's say I have two User and Item objects. The only behavior in the domain between these two objects is that the user may like the item. Since there is no limit to the number of items that the user may like, this many-to-many relationship can be great.

I don’t think it’s reasonable for the user to have a list of elements that they liked in their model or vice versa, since I will have to load a potentially large collection of elements to add just one item. From the point of view of domain design, it also makes no sense for me to refer to the fact that any organization refers to another in its fields, since no behavior requires a collection of elements or users.

I need to maintain this relationship, as the user interface should display a list of elements that the user liked, and a list of users who liked the element. A request can be made using the reading model, but I still need the concept of a domain in order to fix this relationship so that it remains.

One way I can come up with is to introduce an aggregate type of aggregate, such as UserLikeItem, and the user.like (item) method to return an instance of UserLikeItem, which I can then use UserLikeItemRepository to save.

Is this a valid solution? What is the natural way in DDD to model this type of large, but not behavioral, relationship?

+5
source share
3 answers

Since I came across this scenario a while ago, here I take it. The user and the element are part of different aggregates that they do not know / do not care about each other (even if the element has userId). A "similar system" (LS) is a different collection of tracking, similar to , who , what .

LS also does not care about the user or the element, although, since I do not see a case where something other than the user may like something, I can say that Like will always mean userId (not the whole User concept) and the subject ( Item, VIdeo, Picture, Post, etc.).

LS simply supports the association of a user ID and another itemId of a certain type (which can be a string if you want LS not to be associated with what Item is). Every time a user likes a command: RegisterLikeForItem {UserId, ItemId, ItemType}. The LS handler will save this information and then post the UserLikedItem event. One of its handlers will be a counter that will count how many users liked this element. Another handler can make a list of what one user liked (this will be requested by the user interface).

Each handler serves one use case and, perhaps, each has its own repository. I know that this looks rather complicated, but in fact it is simple (one use case requires a processor and, possibly, storage) and all the bets, it is very flexible and supported. And the system works with one or 1000 types of elements.

+2
source

I'll take a dig at this :) IMO, if it’s not a behavior it doesn’t live in a “domain model”, and if the vast majority of your concepts aren’t a behavior, then you don’t need a “domain model”, you should probably look for a script operation like template, not domain model.

I would rephrase your question: “How can I persevere in many ways in many ways” we have several answers to this question, one of which you mentioned, the other can simply store a list of identifiers inside classes.

the domain model and the object-oriented representation of your persistence by the backend are two separate things, the domain model is the behavior first, so you think about the behavior, and then add properties to the domain model that will be affected by this behavior, otherwise what you need is this is an "object oriented representation of your ground state" (DTO)

But it becomes difficult if you have a case for a domain model, but only a couple of concepts are anemic and have no behavior, but this is a question for one more day :)

0
source

'Like' is a fact of the domain that Entity can represent.
Elements have Like → The element contains a collection of Likes.
This collection is only required to add and remove like -. > The item has a LikeCount property and collecting likes is optional for the Repository request (used only in the Item.AddLikeFrom (user) and Item.RemoveLikeFrom (user) method)

It may seem that the attitude is not behavioral, but in fact it is the behavior of one of the parties to the relationship.

0
source

Source: https://habr.com/ru/post/1205741/


All Articles