What design pattern is better to work with controllers / services / DAO

During my career, I saw several different designs, how to work with layers of DAO , Service , Controller . I want to ask about two that are similar but have few differences. The first design creates a visible barrier between the layers. Controllers always use services and only services. Services may use other services or the DAO . Controllers cannot use DAO directly. This design is understandable, but it has a big drawback for me: we always need to create a maintenance method for each method in the DAO . But in many cases, Service methods call only DAO methods and everything else.

For example: We have UserDao

 class UserDao { public List<User> findByName(String name) { ... } public List<User> findByLastName(String name) { ... } public List<User> findOlderThan(int age) { ... } ...... } 

All of the above methods are used by Controllers . What should we do in our case? Create similar methods in UserService :

 class UserService { public List<User> findByName(String name) { return userDao.findByName(name); } public List<User> findByLastName(String name) { return userDao.findByLastName(name); } public List<User> findOlderThan(int age) { return userDao.findOlderThan(age); } ...... } 

For me, this is an extra unnecessary layer for read-only methods.

In the second project, we have no problems with this, because we can use DAO directly in the controllers. In this project, we have a rule: if we want to extract data from the “data warehouse”, we can use the DAO at any level, if we want to make some changes to the “data warehouse”, we must use maintenance methods.

What do you guys think about these projects? Which is better? What should I use in my projects and what should I forget? Could you share with me your experience in this field?

+7
source share
1 answer

Services in MVC and MVC design templates are the "top" of the model layer. This is how the presentation layer interacts with the domain business logic (also, the “reading” part must be performed by the presentation instances).

In this structure, services act as a barrier that isolates the presentation layer and gives you additional freedom in further changing the internal structure of the model.

Another thing you should keep in mind is that there are more structures in it, and then DAOs with which the services interact. First of all, domain objects (business objects, model objects) that encapsulate various business rules related to one specific object. Then you can also have data mappers that abstract storage logic instead of DAO. Or DAOs, which translate information between cartographers and domain items. On a larger scale, the application will also have units (s) of work . On a smaller scale, you will be fine with some active recordings .

We can say that the model layer contains three main groups of the structure in which certain aspects of the model are implemented: domain, memory, and application logic.

Application logic is the interaction between storage domain objects. This is what the services are responsible for.

If you expose a DAO, your model layer begins to flow in the presentation. The controller should have no idea which structures are used and when they are saved.

+3
source

All Articles