mardi 25 décembre 2018

why are service layers good at all?

I know it's a huge text, but thanks so much in advance for reading it. Appreciate that really.

Let's imagine I'm using MVC. controller got the request from user. do the logic in controller and return the response.

People say that controllers don't do any logic, they simply give the incoming request to service and all the logic stays in service classes methods. BUt I don't understand why this is good.

Argument 1) people say this is good because you have skinny controllers instead of fat controllers. but who cares skinny controllers if it doesn't give you benefit?

Argument 2) your business logic is somewhere else and not coupled to controller. Why is this argument even worth mentioning? Okay, I have all the logic in service classes' methods and in my controllers there're two lines of code. what did that give me? Service classes are still huge. Is this some kind of benefit?

Argument 3) one even told me that service layer is good because from service methods you return objects to controller and in controller we sometimes return json or some other format. he told me this is good if we have desktop/web/mobile application all together and we are writing api for them. but still doesn't make sense.

What people do and I hate is that they use repository and service(in service methods, they have business logic and repository classes method calls).

Here is how I think. IF using service classes(I call it helpers), in a service method, there shouldn't be a thing related to framework. if there's freamwork dependent code, then it's bad because all my business logic is tightly coupled to framework. What my friend advised is that I put get,insert,update eloquent calls in controller and pass the results to helper(service) which does the data modification. This way to test a helper(service) no need to inject repository or model at all. and why do we have to even need to test repository or model (it's already tested by the framework).

I need your help, This is the first time I'm really hopeful that people can really help me. I've been thinking about this and I still don't get it for a month already. I even have a deadline for my project and I don't want to write bad code. Project is huge. I just have to understand why service layer is gonna help me. The thing is I've read so much, (so much you can't imagine) and none of the articles really say the real benefits. Is it possible we discuss pros and cons with examples? I'd also love to chat privately to someone with experienced skills. I'd do anything to understand this.

Aucun commentaire:

Enregistrer un commentaire