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?
source share