What's the difference between a Controller and a Service?
Asked Answered
G

2

13

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.

Guerrilla answered 18/9, 2010 at 2:59 Comment(0)
E
8

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

Erethism answered 18/9, 2010 at 3:24 Comment(0)
C
2

If we're speaking about ASP.NET MVC, WebAPI, then my understanding - the Controller would define and expose the API part, while the Service is a kind of a helper for the Controller, e.g. providing methods to access the database, other API, etc.

Checkerberry answered 25/10, 2023 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.