What is the usefulness of the service layer in the Laravel?

Asked

Viewed 969 times

7

What is the real usefulness of a service layer in the structure of Laravel?

It’s just about separating the code from the Controller?

How to use this layer correctly?

My biggest question is whether it is worth increasing the amount of code in a medium-sized application to enter with the service layer. Often small pieces of code can be written to the controller itself to insert/update/delete a record. Only it bothers me to directly access the bank in the controller, so I use services.

But, I think if one part of the application uses one or more services to perform bank operations according to the business rule, all other parts of the application must also use, this includes those actions that have only one line. Am I thinking wrong? Should I use services only in more complex actions (for example, a user registration)?

I’m sensing my codes are getting more verbose because of this.

For example, to update a user name, I could simply do:

function update(User $user) {
   $user->name = 'Foo';
   $user->save();
}

But instead I do:

  function update(User $user) {
    $this->userService->updateUser($user); //Aqui aplico a regra de negócio, num outro método em outra classe, que talvez nem fosse necessária
}

What would be more correct?

  • 1

    It really doesn’t need mainly if it is to make methods that already exist. I think the layer of Model Eloquent already does what it needs and is enough. The only thing I use is to simplify within the Model filter calls, orders, junctions etc ... that is, I can solve everything in the same class, instead of instantiating another ...

2 answers

3


Good morning,

I’ll give you my opinion, but I believe the way for you is to understand why we share the code and why we share it.

In this example you only transfer the data from the controller to the service and there in the service treats the data to persist, which does not bring apparent gain, it seems your service is intended for the persistence of the data in the database, which means that it should not treat any rule but persist the data only, in this specific case you could treat the data in the controller and only use the service to persist.

Another advantage is Dependency Injection, you for example can make this service implement a Persistence Interface, and so replace persistence directly in the class constructor, a very good example is the implementation of Unit Tests, if you want to test this Function, as it is, you must persist in the database every time you run the tests, but if you have in the $this->userService object a received object as parameter in the class construction (dependency injection), you can easily replace the injected class with a class that simulates persistence.

This is just one example, there are many other benefits besides those mentioned, of course.

For some people, several separate files, and small functions may seem verbose and unnecessary, mainly because we tried to think how the machine would differentiate the code, and we felt we were spending 10 lines to do what the machine would understand in 1, but we do not need to write code thinking about the machine, but rather about the people who will read this code, always try to imagine a class or function being read by a human being, totally isolated from code and context, if you can’t understand what the function does in a scenario like this then it’s not good code, don’t forget that programmers spend much more time reading code than writing, then it’s in reading that we have to focus on.

A very cool summary about Clean Code:

https://www.youtube.com/watch?v=9w3o9NHXqu0&list=PLMdYygf53DP5Sc6yFYs6ZmjsuuA2fu0TK

1

Well, I think the great advantage of this layer is actually making your code much more reusable and solid. This regarding the functionalities of your application are well divided, in classes with well defined behavior, and methods that do exactly their specific task.

As for example in your code above, where the Controller only makes a manipulation of the task to be performed, obtains the return by half of a class of Servico, which, as its given name says, must have the services related to that entity only in the case of User.

This way, if at another time you need to perform some service related to the entity, you can make direct use of the class that contains the entity’s Services, thus making your code much more reusable.

I hope that was helpful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.