what is a service and what is its difference to a controller?

Asked

Viewed 3,026 times

4

I have read many articles and seen many lessons on Java and lately I come across a lot with the concept of services and controllers, I have the clarity of the role of the Controller within an MVC structure, but I have some questions about the role of the service within a project:

What would be the role of the service exactly ?

What exactly differs a service from a controller ?

Where did this service concept come from ? ( Part of some new architecture ? )

Do you have any example of a project that clearly shows the difference between one and the other or any study material that explains this in more detail ?

I found some answers in the global Stack Overflow, but nothing that made that clear to me.

  • 1

    Briefly, the controller is responsible for receiving an HTTP request and generating an HTTP response... and only. All the logic of the application must be in another structure, usually in the service. This stereotype runs away from the standard that the controller calls the model. Here the controller calls the service and the service that calls the model and thus you are free to perform the same action from different sources that are not HTTP requests, such as a CLI call, for example.

  • I think I’m beginning to understand the concept, is that I’ve always used Resources to expose my endpoints and it passed the generated objects to the controller and that did its job, so different forms of HTTP access, gRPC among others all had a specific package that took care of the serialization of the data to be sent to the controller.

  • In this case who is responsible for all the flow that was previously the controller is the service and the controller’s work is like something more generic like the Resources package that I mentioned ?

1 answer

7


The role of what we call controllers is to orchestrate the data received by your Java application. It should handle system entries with some checks, such as data types, for example if a mandatory parameter was sent in the request. After these checks, the Controller must pass the data received by the request to what we call Services.

Services are responsible for the business logic of your application and are responsible for communicating with the most internal layers of the Software, such as a Data layer.

The concept of service layer is not new, it has been used in the market for some time. It arose from the need for a layer responsible for making the business rules negotiations, trying to decouple the Controllers layer from the Models layer. This layer separation is one of the ways of architecting your application, but there are many others, and they are used to meet non-functional requirements such as maintainability, reusability, performance, etc.

For example, in this image we can see a layered architecture called Clean Architecture. I recommend reading Robert Martin’s book Clean Architecture for more details.

Clean architecture

  • If I’m not mistaken this architecture is the same as the right Hexagonal architecture ?

  • I really liked your explanation and it was clear now the role of this layer and the division of tasks between the controller and service

Browser other questions tagged

You are not signed in. Login or sign up in order to post.