B
B
Biaci_Anj2022-02-20 23:14:42
Java
Biaci_Anj, 2022-02-20 23:14:42

Is it normal to call DAO and Service from Servlet at the same time?

I am writing a servlet project, I have done it this way, if the method needs a simple action with the database -> DAO is called.
If you need something serious (save a photo, for example) -> service.
At the same time, the service of one entity is generally empty, there is nothing there.

Is it normal? Or do I have to make a gasket in the form of a service between the servlet and the dao, which simply delegates all the affairs of the dao?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2022-02-20
@Biaci_Anj

I'll tell you from personal experience:
I always want the model that is used in the database and the model that is used in the controller to be the same model.
Everything is simple: a request came to the controller - save it in the database; you need something from the database - make a select and send it to the controller.
But never, never was it a good idea.
Let's start with the fact that as soon as at least some nesting of objects appears, no one in their right mind will pass these objects in the controller, limiting themselves to links to them, at best (using their id); Let's finish with issues of security and consistency of data that are sent to the database immediately, past validation and some kind of service.
I answer your question:
1. Always use a service. The ability to save objects in the database of the controller is contrary to the solid
2 principle. Use the onion architecture. The domain model should not know anything about external integrations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question