Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API, where to put private methods? [closed]

So I was wondering the other day, where to put private methods, that do some dirty work in Web API.

I need to extract certain things from JSON, methods will do the job and return some result.

So where do I keep those methods? do I need to write a separate library (dll)? or just do this stuff in the controller?

like image 977
CrushJelly Avatar asked Apr 25 '26 06:04

CrushJelly


2 Answers

I don’t think it can have a single answer – it depends on…

If you think this private method can be reused from some other controller in future, better to have a separate class, if you think it can be reused from separate modules (not just from controllers), a separate class library project can be the answer.

But if you consider this private method is designed to support for a specific action of a controller, you can write within controller, before taking any decision a few more parameters to be considered like unit testability or slimness of API etc.

like image 50
Sudipta Kumar Maiti Avatar answered Apr 27 '26 20:04

Sudipta Kumar Maiti


You should keep your controller thin as much as possible. You can move logic code to service package, for example JSONService class or JSONLib. I often use following layout:

controller/
lib/
service/
model/
like image 40
hainguyen Avatar answered Apr 27 '26 20:04

hainguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!