r/SoftwareEngineering • u/SiegeAe • Dec 12 '25
When would service layer, single line methods be justified?
Are there practical reasons for having several methods in the service layer, of the typical controller-service-repository structured codebases that are simply one line for delegating calls to a repository method?
Its common to see people follow "best" practices without seriously considering the intent, so I have a suspicion this might be just a case of that happening but want to figure out where I might be wrong, one that's struck me recently is this trend to have some service calls that do nothing but delegate to the repo layer, no branching, sequences or even any guards. When I asked why these particular cases were there they said simply "not to call the repository from the controller" which came across as bit of a "just because" reason at face value.
For me I take them as a sign that there's probably either some bloated controller methods or that the service methods should just be removed until there is a need for some type of translation or guards between the controller and the repo, am I missing something obvious here?
5
u/davy_jones_locket Dec 13 '25
It is definitely a sign that your controllers are bloated and probably doing too much, the wrong separation.
Typically the point of having the service layer between the controller and repository is to do things like
Plus your services aren't always going to be 1:1 with a repository anyway, and the controller shouldn't concern itself with the details. Your service should be able to call multiple repositories for related data and coordinating with other service calls.
Plus it forces you to keep business logic out of the HTTP layer. The whole point of not calling repository from controllers isn't "just because" but because the controller doesnt need to handle business logic. If you're putting business logic in the controller, it's gonna get bloated and messy.
If you have thin service methods, it's probably a sign that your controllers are doing too much business logic now anyway. Controllers should be thin, punt the complex business logic to the service layer. Or it could be that your business logic isn't that complex anyway. But just because you can call repository methods from the controller doesn't mean you should. The whole point is to keep the business logic contained in a separate layer.
2
u/ZelphirKalt Dec 13 '25
It would be justified for consistency. If everything else is routed through some layer, then you don't suddenly want to have one call not going through that layer.
1
u/PickleLips64151 Dec 12 '25
Are you referring to the facade pattern?
2
u/Big-Moose565 Feb 15 '26
A lot can go into a single line, so it depends what it's doing (its responsibility).
Aside from that, if it's a generic line, the only time I'd consider it acceptable is to make testing easier, which the testing would drive.
Beyond that I only break out a new class/module when needed (again driven by testing complexity).
I'm all for consistency, but applying a facade pattern even when not needed, just because other parts of the code uses it - just highlights the pattern doesn't really fit. Nothing worse than forcing a pattern (square peg, round hole).
1
u/Separate_Earth3725 Dec 13 '25
Does the service only call one repository or does the service act as a gateway to multiple repositories? Kinda like an aggregator service that puts contextually similar repositories in one place?
1
u/bluemage-loves-tacos Dec 15 '25 edited Dec 15 '25
Is the repository internal to the domain? The service layer is there to make internal refactors easy as it's the communication between internal and external. If everyone and their dog is accessing internal objects (repositories) externally, you get a spaghetti code mess, and refactoring anything internal becomes a big pigs ear of a project. If the service layer is used, the internal refactor is easy as you can do it without breaking the contract. It may look like a waste if the repos are just being called for a single thing, but that's an internal concern, and the consumer has no need to even know that's happening.
If the repos are external objects, then you have two problems. You have a duplication of effort in managing the services and repositories AND you have two sources of truth to manage. Just get rid of one, and I don't mean in just the circumstances the services are calling the repo directly, I mean either get rid of the services or the repositories as they're doing the same job.
ETA: I wouldn't ever advocate for removing the services in favour of repositories, because then you're forcing repositories to BE the service layer. The abstraction still exists, it just now has a confusing name and will, over time, become services anyway, potentially adding in a bunch of further badly named abstractions to re-create the repositories, but not called repositories to work around it. I would rather ditch the repositories and get the services calling the data layer directly.
1
u/Diligent-Offer4540 Mar 14 '26 edited Mar 14 '26
IMO it is a good practice because it centralize the logic.
Even if for now it is very simple (only one line) maybe in the future this function will have assertions or other important business logic.
If you used this service function elsewhere it will automatically apply this logic to all occurrences in your codebase.
In the case you didn’t have this function you would check in your codebase where you should apply the new logic which isn’t straightforward and you can miss some cases.
1
u/shoe788 Dec 13 '25
Perhaps earlier on in a project that is expected to get much more complex over time
1
u/[deleted] Jan 21 '26
[removed] — view removed comment
1
u/[deleted] Jan 27 '26
[removed] — view removed comment