DDD return aggregate entity of an AR

4

I am with the following doubt that no post I researched answered me. And I've done a lot of research. I think it's a simple question, for being a common case.

Imagine a blog posts 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 post comments?

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

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

    
asked by anonymous 10.09.2015 / 16:36

2 answers

1

In fact the entity should rather have the comments attribute, as it is part of the aggregate. As I was using PHP, my question 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 in the database, and this really helped. Then I used Doctrine 2 to map my entities to the tables, which solved the whole problem. That way my domain has been totally separated from my infrastructure.

I leave here the link from a series of posts that helped me a lot (I'm leaving the page that starts the posts, more precisely in the article Encapsulating your application's business rules) and uses PHP and Doctrine.

    
03.12.2015 / 18:03
1

DDD is not for developing a blog

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

If we insist on using DDD in a blog, we will realize that comments are not entities but rather value objects because a comment does not have 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 the comments would be through a post method (the getComments 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 type of problem you're facing by trying to use DDD in this case:

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

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

This would do 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 these options would seriously violate the DDD principles).

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.

    
03.12.2015 / 18:34