Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of service layer in Spring Boot applications?

I am new to Spring Boot and I am creating a RESTful API with no UI.

I am thinking if I should use business service and call repository from there or just call the repository directly from my REST controller?

like image 380
bee Avatar asked Oct 04 '19 10:10

bee


People also ask

What is the use of service layer?

A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer. The service layer contains business logic. In particular, it contains validation logic. For example, the product service layer in Listing 3 has a CreateProduct() method.

Is service layer mandatory in Spring Boot?

Service layer is not a concept exclusive from Spring Boot. It's a software architectural term and frequently referred as a pattern. Simple applications may skip the service layer. In practical terms, nothing stops you from invoking a repository method from the controller layer.

What is the purpose of service layer in Java?

The service layer consists of a collection of Java classes that implement business logic (data retrieval, updates, deletions, and so on) through one or more high-level methods. In other words, the service layer controls the workflow.

Why do we create service layer?

The Service layer's single responsibility is to do any logic required with the data received by the Controller. The repository's single responsibility is to query the data base.

What is service layer and DAO layer?

Generally the DAO is as light as possible and exists solely to provide a connection to the DB, sometimes abstracted so different DB backends can be used. The service layer is there to provide logic to operate on the data sent to and from the DAO and the client.

What is the use of @service annotation in Spring Boot?

Spring @Service annotation is a specialization of @Component annotation. Spring Service annotation can be applied only to classes. It is used to mark the class as a service provider.


2 Answers

Separation of concerns is the key:

  • The controller (presentation layer, or port) is a protocol interface which exposes application functionality as RESTful web services. It should to that and nothing more.
  • The repository (persistence layer, or adapter) abstracts persistence operations: find (by id or other criteria), save (create, update) and delete records. It should to that and nothing more.
  • The service layer (domain) contains your business logic. It defines which functionalities you provide, how they are accessed, and what to pass and get in return - independent on any port (of which there may be multiple: web services, message queues, scheduled events) and independent on its internal workings (it's nobody's business that the service uses the repository, or even how data is represented in a repository). The service layer may translate 1:1 from the repositiory data, or may apply filtering, transformation or aggregation of additional data.

The business logic may start simple in the beginning, and offer not more that simple CRUD operations, but that doesn't mean it will forever stay this way. As soon as you need to deal with access rights, it's no longer a matter of routing requests from the controller directly to the repository, but checking access and filtering data as well. Requests may need validation and consistency checks before hitting the database, rules and additional operations may be applied, so your services get more value over time.

Even for simple CRUD cases, I'd introduce a service layer, which at least translates from DTOs to Entities and vice versa.

Keep your controllers/repositories (or ports and adapters) stupid, and your services smart, and you get a maintainable and well-testable solution.

like image 121
Peter Walser Avatar answered Sep 27 '22 23:09

Peter Walser


Service layer is not a concept exclusive from Spring Boot. It's a software architectural term and frequently referred as a pattern. Simple applications may skip the service layer. In practical terms, nothing stops you from invoking a repository method from the controller layer.

But, I strongly advise the usage of a service layer, as it is primarily meant to define the application boundaries. The service layer responsibilities include (but are not limited to):

  • Encapsulating the business logic implementation;
  • Centralizing data access;
  • Defining where the transactions begin/end.

Quoting the Service Layer pattern from Martin Fowler's Catalog of Patterns of Enterprise Application Architecture:

A Service Layer defines an application's boundary and its set of available operations from the perspective of interfacing client layers. It encapsulates the application's business logic, controlling transactions and coor-dinating responses in the implementation of its operations.

like image 36
cassiomolin Avatar answered Sep 28 '22 00:09

cassiomolin