Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a Controller and a Service?

I'm looking for how to structure the layer of my app between the presentation layer and the model / business object layer. I see examples using Controller classes and others using Service classes. Are these the same things with different names for different methodologies, or is there a more fundamental difference?

Edit: To put the question in context, this is a PHP app using Doctrine as the ORM.

like image 463
BenV Avatar asked Sep 18 '10 02:09

BenV


People also ask

What is the difference between service and controller in Java?

Their only difference comes in their purpose i.e. @Controller is used in Spring MVC to define controller, which are first Spring bean and then controller. Similarly, @Service is used to annotated classes which hold business logic in the Service layer and @Repository is used in Data Access layer.

What is a controller in a REST API?

RestController is a Spring annotation that is used to build REST API in a declarative way. RestController annotation is applied to a class to mark it as a request handler, and Spring will do the building and provide the RESTful web service at runtime.

What is the difference between controller and middleware?

As a rule of thumb, middleware are often reused more than once and often they do not response. On contrary, controller respond and are most of the time specific to one endpoint. 'Controllers' aren't first order components of node/express. They are project artifacts if defined as such.

What is the use of controller service and repository?

Controllers - contains application logic and passing user input data to service. Services - The middleware between controller and repository. Gather data from controller, performs validation and business logic, and calling repositories for data manipulation.


1 Answers

I would say terms like Controller are basically same names for potentially very different things depending on what methodology / framework you are using. At a very high level, they may perform the same action - hence the generic name usage - but their responsibilities and scope within the context of the framework will usually be much more specific and different.

Eg: The Controller in MVC has little or nothing in common with the Controller layer in WCSF.

I think these terms like Controller / Service etc are generic and hence have been used in many frameworks but they have a special meaning within the framework of reference.

Also, specifically, a controller and a service to me are two completely differing concepts.

Controller is something like a layer that is responsible for orchestrating logic within the application / or an aspect of the application

Service , to me, is basically the external API through which you expose aspects of your application in a standard manner

like image 84
Jagmag Avatar answered Sep 24 '22 16:09

Jagmag