D
D
Dmitry Bashinsky2020-07-21 11:35:07
C++ / C#
Dmitry Bashinsky, 2020-07-21 11:35:07

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.
Jnpo46d.png

I implemented a page method for adding a comment and then created a command handler for adding a comment. The command comes to me
2SLvmD9.png

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

1 answer(s)
E
Evgeny Romashkan, 2020-07-21
@EvgeniiR

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));
  }
}

In the context of comments, the article is not necessarily needed - only its identifier is needed.
Also, in the context of comments, user data is not needed at all - only its identifier. Identifiers are stable enough information not to be afraid to rummage around them.
And yes, in this case we are simply dealing with the decomposition of the system, this has little to do with real DDD, because domain-driven-design, it is domain-driven due to the fact that the contexts are discussed with the business, and not invented by the developer, in this In this case, we just take a little bit of terminology from there, because it has already become quite general.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question