Also, that the aim beneath DTO's is to gather in a single response as much info as necessary for clients to save calls to the server.
That's not the aim of a DTO. It's a side effect that you can more easily achieve when already using DTOs. But using DTOs and bundling data are two different design decisions that should each be made for their own reasons.
What I am having a hard time with is understanding what is the best situation to use DTO's compared to use of entity?
In essence, an "entity" is just shorthand for "the DTO between the data store and our persistence logic". It's a DTO just like any other DTO, meant to be used for data transfer between two specific points.
Depending on the two layers that are passing data to one another, these DTOs tend to get a contextual nickname:
┌───────────┐ ┌──────┐ ┌───┐ DB ─┬─►│Persistence│◄─┬─┤Domain├─┬─►│Web├─┬─► END USER │ └───────────┘ │ └──────┘ │ └───┘ │ │ │ │ │ "Entity" "Domain "Business "Model" Object" Object"
On a technical level, all of these intermediary objects are DTOs. We just give them different names so it's easier to talk about them and understand what someone means when they say "entity", without forcing them to have to say "the DTO between the database and the persistence layer".
This also inherently explains why/when to use an "entity" specifically. Entities are used as a means of communication between the database and the persistence logic. If you're passing data between different layers, then it shouldn't be handled by an entity.
Very important note Your specific architecture will change what layers you have and what the DTOs are commonly called. I used a DDD example which relies on an ORM here, but you might be using a different architecture altogether, which would mean you need to adapt the names accordingly.
For example, if you don't use an ORM and write SQL queries manually, then you might refer to the Persistence->Domain DTO as the "entity", since you don't actually have a DTO class between DB->Persistence.
For example when I am trying to save new user in database should I use DTO or entity?
It's a staged process.
- The web controller receives a model, maps it to a business object, and passes it to the domain.
- The domain receives the business object, translates it into a domain object, and performs the operation. At some point, this means passing (some of) the data to the persistence layer.
- The persistence layer receives the domain object, maps it to an entity, and sends that entity on to the database.
- (Commonly hidden step: your ORM maps your entity to a SQL command and sends that to the database)
For retrieving data from database and communicating with frontend in that case only use of DTO's?
This is the same staged process, albeit in the opposite order.
Hoewever, there may be contextual differences. For example, if your retrieved data doesn't retrieve full entities (i.e. table rows), but instead is more of a "report" of very specific data columns (maybe even some calculated columns), then this object returned from the DB is generally not even called an "entity".
"Entity" generally refers to the class that represents a full table row.