DDD return aggregate entity of an AR

Asked

Viewed 125 times

4

I am with the following doubt that no post I researched answered me. And I have researched a lot. I believe it is a simple question, because it is a common case.

Imagine a blog post system. In this case I have the Post Entity, which has the Comment entity. My doubts are as follows:

1 - The Post entity must have a comments property and in this case have a method called getComments() to return all comments from the post?

2 - If not, how should this be done? In a Postrepository?

I am very confused about these concepts. Code examples can be made in any language.

2 answers

1


In fact, the entity should have the comments attribute, because it is part of the aggregate. As I was using PHP, my doubt was how it would be done to relate this to a database without mixing infrastructure and domain.

First I realized the real meaning of not creating the domain thinking about the database, and it really helped. Then I used Doctrine 2 to map my entities with the tables, which solved the whole problem. That way my domain was totally separated from my infrastructure.

I leave the link to a series of posts that helped me a lot (I’m leaving on the page that starts the posts, more precisely in the article Encapsulating your application’s business Rules) and uses PHP and Doctrine (in English).

1

Ddd is not for developing a blog

I understand you’re trying to exercise the concept, but a blog is not a good context to exercise DDD because a blog is too simple a domain - it does not offer the kind of complexity that DDD seeks to solve.

If we insist on using DDD on a blog, we’ll realize that comments are not entities but yes valuables because a comment has no identity and has no relevance in the domain if it is not preceded by the post.

This put; yes, in DDD the correct way to get comments would be through a post method (the getComentarios that you mentioned).

But I repeat: this is not a valid example of DDD.

Using DDD to develop a blog brings more problem than solution

See the kind of problem you face trying to use DDD in this case:

When a post is very successful and receives 900 comments, getComenters will become a slow method and the rendering of a page with 900 comments at once will also be too slow.

If you want to implement paging in getComenters, you need to do this in another layer (in the application or below) because according to the DDD this implementation detail can not obfuscate the domain code.

This would take a lot more work than simply implementing paging by getting comments directly from a repository, a DAO or even directly from the database (all of which would seriously hurt the principles of the DDD).

And this additional work would bring no benefit because as Post and Comments are very simple artifacts, they will not enjoy the benefits of such careful abstraction.

  • 1

    Yes, I agree with you. In my question I only used to exemplify, I’m not using Ddds for a blog, I’m using them for a system that actually has business rules and tends to grow a lot. But you touched on an important point, which is the question of performance, mainly in web systems. Thank you for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.