Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call service vs dao from another service

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?

like image 547
Erlan Avatar asked Aug 17 '15 11:08

Erlan


People also ask

What is difference between DAO and service?

The definition of a DAO is a company that operates without any human involvement. The difference between the two is that a DAO does not have labor while a service has human labor. The difference between DAO and service is that a DAO is an organization without a central manager.

Should a DAO call another DAO?

Neither Dao should access the other one. If there is some logic that needs to manipulate the two, then that has to be in your application logic, not in Data Access Layer. Address is a value type associated with Customer: In this case, address does not have a separate DAO of itself.

Can you use @service over a DAO?

In your scenario, it has no impact on application flow whether you use @Service or @Repository, application will work as they are elligible for autowiring. But standard practice is to use @Repository for dao classes.


2 Answers

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 )

like image 178
Anudeep Gade Avatar answered Sep 22 '22 20:09

Anudeep Gade


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.

like image 41
cahen Avatar answered Sep 20 '22 20:09

cahen