Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service layer in Java or Only Repository

Tags:

java

spring

If i have Spring data repository which includes save, delete and modify operations. Should I create the following methods operations in service layer? And adding annotation @Transactional? Or leave them as is? Or should I have them in repository layer?

Example service

@Service
public class RepositoryOperations{

@Autowired
ProductRepository productRepository;

@Transactional
public void saveProduct(){
    productRepository.save();
    }

...

}
like image 919
Marcel Radek Avatar asked Jul 07 '17 19:07

Marcel Radek


2 Answers

I think that you are using a good approach for your code. You can create a service layer that will invoke the product repository methods in order to separate responsibilities (business from data logic).

A 3-layered architecture describes your approach as follows:

  • Controllers: Handle all the input values that enter to the system (from endpoins, for example) and passes them to the service. In Spring you can use the @Controller class annotation in order to differentiate them from other components.
  • Services: Components that handle all the business logic related to your application. You can use the @Service annotation.
  • DAO (Data Access Objects): Classes that contains the data logic (in your case, the repositories). You can use the @Repository annotation.

In order to prevent any inconsistent in your saveProduct() service method when a problem occurs, you can use the @Transactional annotation in order to rollback all the changes made by your method in the database, so i consider it's a good practice use that annotation when you are working with writing operations in database.

like image 102
Andrés Felipe Pedraza Infante Avatar answered Sep 30 '22 12:09

Andrés Felipe Pedraza Infante


We keep three layers generally.

  1. The controller which defines API endpoints and calls appropriate service to process the request.
  2. The service layer contains business logic and is meant to do heavy processing as we call it. If service layer needs to save or retrieve data from the database, it will use the dao layer to achieve this.
  3. Dao layer's only role is to save or retrieve data from the database and provide it back to the service layer. It should not contain any business logic.

Annotate your dao layer with annotation @Repository
Annotate your service layer with annotation @Service
Annotate your controllers with annotation @Controller or @RestController(in case you have REST APIs)

@Transactional annotation is used when you want a code to be rolled back if it fails. If your service places three dao calls for three different operations and you want all or none of those three operations to be performed, annotate your service method with @Transactional
Same applies for dao methods.
So you have now fair amount of information to decide upon.

like image 42
rohanagarwal Avatar answered Sep 30 '22 13:09

rohanagarwal