B
B
Biaci_Anj2022-04-11 12:25:46
Java
Biaci_Anj, 2022-04-11 12:25:46

Why is DTO needed in simple servlets?

Could you please explain whether it is necessary to use DTO in servlets?
I'm trying to understand logically why
I get the Parameters like this and immediately make an Entity.

String name = req.getParameter("name");
        String description = req.getParameter("description");
        String category = req.getParameter("category");
        String price = req.getParameter("price");

What would be the point of creating an DTO here first and making an Entity out of it?
In spring, I can understand that we use annotations there and the object must be a request.
And then we get each parameter ourselves.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2022-04-11
@Biaci_Anj

See, an entity in a database can contain many more fields and objects than you want to pass up or receive as a form.
As an example, displaying user information. The entity will most likely contain fields about the password hash, ackles (ACLs), additional links with other objects. On the top, most likely, this information should not be transferred, this is the inner workings of your application. To do this, they create a DTO (data transfer object), in which there is exactly what needs to be transferred. And yes, you need to do merging or encapsulation between entity and dto every time back and forth. On the one hand, there are unnecessary actions and transformations from objects to objects, on the other hand, there is a guarantee of security and separation of the logic of storage and the logic of transferring entities.
Whether it is necessary specifically in your application - no one will tell except you. But, it's good practice to make a dto for every case.

N
nApoBo3, 2022-04-11
@nApoBo3

When you need to change the object in the database, add an intermediate layer of logic or supplement the transmitted data, you will understand why the dto object is needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question