9

I have User And Role entities and Service, DAO layers for them. I need Role list from UserService.

Which layer should I use from UserService? Call list method of RoleService vs RoleDAO? Which one is common use and why?

Erlan
  • 2,010
  • 1
  • 22
  • 31

2 Answers2

15

Normally DAO layer is close to database, Service layer is encapsulating your business logic, performing any transactions or other things rather than just calling DAO.

Service calling another service is more common because

  1. Your RoleService can have some business code evaluated, you can benefit from transactions or passing messages via JMS or you can have some security on service methods in future. So separating concerns is good practice.

  2. Easy to mock the services and test ( this can be argued even DAO can be tested), but separating business logic is good way by using service layer interfaces.

But if you dont have any business logic in service layer, you can avoid redundant code by simply using DAO (but for the future you will have a code debt for refactoring if you think of service layer business )

Anudeep Gade
  • 1,365
  • 1
  • 9
  • 18
3

Call the list method in the RoleService.

The business logic around Roles may change one day and all changes in the RoleService to handle that will be useless for all code calling the DAO directly.

cahen
  • 15,807
  • 13
  • 47
  • 78
  • Yes, I thought the same way first. But Service was calling it's DAO, and I thought its maybe good idea(may there be some reasons ) to call DAO's only from service layer because service layer is used for calling dao. – Erlan Aug 17 '15 at 12:14
  • 1
    services calling services is totally fine – cahen Aug 17 '15 at 12:22