ServiceLayer — what is it?

All found the descriptions says about some high matters. And how it should look in the code - is unclear.
How does the interaction with the ServiceLayer in the controller?
July 12th 19 at 16:40
3 answers
July 12th 19 at 16:42
Apparently nobody understands what it is and what to eat.
Found a couple of presentations. English none, but the code examples are clear.
here is a video to the first - sonya_Schimmel commented on July 12th 19 at 16:45
July 12th 19 at 16:44
ServiceLayer - literally service level. It is assumed that the controller only accesses the services that perform the business logic.
For example - there is an action that accepts a purchase requisition user. We want:
1. to reserve items
2. invoice for payment
3. to send a notification to the user by email
We can write all the logic directly in the action, but it will lead to heavy code duplication and confusing logic. Instead, we will create separate services for each activity( interaction with email, transactions in goods, transactions with payment system).
In the body of the controller, we will give the necessary data to these services and process possible errors. Then return to the user the result.
From your description it follows that the servicelayer is just a set of classes which are usually called models. - sonya_Schimmel commented on July 12th 19 at 16:47
: model - pattern that is used to refer to some entity(usually to build relationships between models and their representation in the database). For the model there may be a repository that performs operations on the model. In this case, our service level will be the repository that allows you to perform operations on the model. - waylon.Turcotte commented on July 12th 19 at 16:50
- the model is a class which contains the data+ceteri+ hetaeras+filter+validation and nothing else and knows nothing. And the class that you call the repository( although this word seems to me a kind of repository) that implements all the business logic, creates the model interacts with the database, populates the model data, stores the model in database, etc. - rodger.Schmeler96 commented on July 12th 19 at 16:53
no. the repository does not "implements all the business logic". the repository is something like a layer which is chasing objects in the database and back with the use of patterns such as AR or DataMapper

and services - this is business logic, which should not be in the controller. - waylon.Turcotte commented on July 12th 19 at 16:56
under the operations podelu I mean CRUD operations. As a rule chain looks like this: Controller -> service -> repository. Sometimes you can omit the intermediate step with the service(for example, if we delete some entity that does not depend on anything, no changes in the related objects does not occur,i.e., the business logic itself is not): then get a controller -> repository. - baron.Li commented on July 12th 19 at 16:59
July 12th 19 at 16:46
Example: to generate a UUID, stored in database and give to the client.

Service UuidGenerator - knows how to create a UUID.
Service UuidDbSaver - able to save the UUID in the database.

Controller: receives a request calls UuidGenerator, it pushes in and returns UuidDbSaver.
And so?
The controller accesses the service UUID "create and save" which in turn uses UuidGenerator and UuidDbSaver. - sonya_Schimmel commented on July 12th 19 at 16:49
And actually what you're talking about like services in the context of the Sevice Locator. But we're talking about Service Layer - waylon.Turcotte commented on July 12th 19 at 16:52
And so it makes no sense))
The service is very limited in functionality thing. It's fairly independent.
What if you have 2 database and system casi? For all to write in the service of generating and saving UUID?
No, you write 1 service know how to generate a UUID and then use it for all other parts of the system. - rodger.Schmeler96 commented on July 12th 19 at 16:55
service locator is a pattern, which is a class that knows how to obtain needed services upon request. Service layer does not change in any way from the fact whether you use a service locator or dependency injection or some other method. - waylon.Turcotte commented on July 12th 19 at 16:58

Sevice Locator - is a method of identifying a service based on a key.
Service Layer - this services And how they are arranged (it may be IoC, ServiceLocator, other subtypes DI, etc.) - baron.Li commented on July 12th 19 at 17:01
In the context of the pattern Sevice Locator service(dependency) is a class which provides some functionality to another class client.
The term Service Layer occurs in the context of the idea of DDD. - rodger.Schmeler96 commented on July 12th 19 at 17:04
Or differently. Service Locator is a specific way of interaction classes, i.e. the pattern. A Service Layer is the idea (not concept) which can be arranged in different ways. The question was how to organize the idea in the form of specific classes. - waylon.Turcotte commented on July 12th 19 at 17:07

Find more questions by tags PHP