Let’s take this scenario of a blogging application.
We have three modules:
Now, comments aren’t limited to just blogs — they can also be made on user profiles and potentially other entities.
For example, I want to build an API for creating a blog comment:
POST /blog-comment
Body:
{
comment: string,
blogId: number
}
Here’s an intentionally odd feature to simulate complexity:
When a user posts a comment, it should receive a comment number, calculated as the total count of all comments across all blogs of the blog’s author, plus one.
So, if the author has two blogs with two comments each, the next comment’s number would be 2 + 2 + 1 = 5.
To implement this, I’d write a method inside the comment service. But for that, I need to query the Blog table to find the author, then get all of that author’s blogs, and finally count their comments.
This raises the question:
- Should the comment service communicate with the Author service and Blog service, making multiple database calls?
- Or should I query the database directly in one go, pulling in all the required tables myself?