Answer the question
In order to leave comments, you need to log in
How to use DDD?
Hello. I have a terrible problem, I do not like the code that we write most often, now I will describe this approach.
We use EF and describe the entities so very similar to the domain, then services are added to these entities that randomly change them and that's it, the application is ready.
I want to apply the practice of DDD, but I get to the point where it's just not optimal to use it. And I'm not talking about how the code looks, I'm talking about the fact that there is a moment where I start to pull out too much from the database, when in fact I need to add 1 row to one table.
If I had done as before, I would most likely have finished, but now I can say that I practically did not start.
I will describe my domain
I have a page, the page has comments, these comments can be left by users.
I implemented a page method for adding a comment and then created a command handler for adding a comment. The command comes to me
And if I do it in the most optimal way, then I would simply add 1 row to the Comments table through EF, but if I go without cheating, but through the domain, then I get the page by Id and I need to add a comment to it, in the domain I have an Author in the comment and now I also take the user by id and add it to this comment.
What confuses me?
Instead of putting 1 new row in the database, even without additional requests, I have all the necessary data, I get the page by Id, the user by Id, create a new comment by inserting the user into it, then I put all this stuff into the page and save.
I want to say right away that I was already offered instead of referring to another entity (for example, a user) I'd better use UserId when I did this, I got a copy of my database schema, but not my domain
Answer the question
In order to leave comments, you need to log in
Let the article and comments be different aggregates. Comments will have their own domain.
You can make your own article class ( Article ) in the context of comments. It won't even be an entity:
class Article {
private UUID id;
private CommentsRepository comments;
...
public void function addComment(commentData: commentData) {
this.coments.add(new Comment(this.id, commentData));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question