Question: Question:
Please tell us about the role of the service.
I learned MVC with CakePHP, but I don't know what role it plays because CakePHP doesn't have a service.
The controller extracts the necessary processing for the request from the user and
Views are what you see to convey results, etc.
We recognize that the model is a collection of processes requested by the controller.
(Is it wrong?)
I recently learned that there is a concept of service, but is this something that can be further divided into models? Or are you talking about the model?
I would be grateful if you could tell us about services in an easy-to-understand manner.
Answer: Answer:
I think that other people's answers will be helpful for one way of thinking in one framework called CakePHP, but take a step back and what kind of relationship is generally MVC
andサービス
, respectively. I will answer in terms of the meaning of the words.
The question says about MVC:
The controller extracts the necessary processing for the request from the user and
Views are what you see to convey results, etc.
We recognize that the model is a collection of processes requested by the controller.
This classification is not so wrong. However, in reality, there are various types of "processing required by the controller". for example,
- Save information in DB / Get information from DB
- send mail
- Create a thumbnail of the uploaded image file
there is. You don't know what these models are, what business logic is, and what services are. Let's organize the words.
What is a model?
In general, a "model" is an easy-仕組みや構造を分かりやすく表したもの
. If it is a system for business, it may be the "information structure" handled in the business, or the "rules for creating information" and "procedures".
What is CakePHP's Model?
In the case of CakePHP's Model, the schema of the database is automatically read and the records in the table can be easily handled, so at first glance the role of Model looks like "code storage for DB input / output". I will. However, the original purpose of ActiveRecord like this is to allow application code to handle the情報の形
unified format.
Even in other MVC frameworks, the part named Model is given the purpose of expressing the "form of information" in this way. The processing that accompanies the shape of this information (input / output of information, checking the consistency of the shape, and some deformation / composition of the shape) will be described in the Model.
What are the rules and procedures?
Some of the first examples were rules and procedures. Are these models, that is, those that accompany the form of information?
A common example is the procedure when a member purchases a product. Check the purchase limit for each member, then check the inventory, and so on. In some procedure, you have to deal with multiple pieces of information across the catalog.
There is also a way to cut out what was described in the Model based on the standard of "handling multiple things" in some other form, but more correctly, "procedure = make the part representing the use case independent". Become. This is one form of using the service described below.
What is a service?
A service is something that処理をするモノ
.
For example, if your application hashes and records a user's password, let's say that the hashing process is independent of one class. This class is the "PasswordEncoder service". In short, you can say that the class that provides the processing, whether it's a library or anything, is a service. Also, this PasswordEncoder service may be used inside the Model when storing user information, or it may be used by the Controller for other reasons.
Scene to use the service
In terms of how to use the service, as was the case with Password Encoder earlier, there are several possibilities.
- Used to summarize the common processing described in the Controller from some point of view
- Used to summarize the common processing described in Model from some point of view
- Used to summarize the common processing required for the template from some point of view
For common processing of Controller, there is a method of creating a base class of Controller and creating a common method there, but this is not a very good method. Instead of reducing the code from each Controller, the complexity is concentrated on the base class side.
If you have a framework that can handle "services" directly, you can use it with the common procedure of simply creating a class corresponding to each process and instructing the framework to use it where needed (for example, Symfony). Service container). In the case of CakePHP, there are individual mechanisms such as helpers depending on where you use it, but if you go beyond that and make it possible to use your own classes, the range of correspondence will expand in various ways. increase.
Is the service a Model?
As in the previous section, it is possible to combine any part of the MVC part into a service. A service is just a "provider of processing". In this sense, a service is basically something that has nothing to do with such a structure as a Model or a Controller.
Service layer (use case layer)
- It is sandwiched between Controller and Model and used like a layer that organizes Model operations.
There is also a usage called. This is not to be used as a component, but to add a structure to the MVC architecture (how to make it). In technical terms, it's called the service layer, or use case layer. This way you can improve the visibility of your production code.
Microservices?
The service expressed by the word "microservices", which has become a hot topic these days, refers to a larger unit than the "one class" described here. For example, the "membership management service" in one company is considered as one service.
The word "service" is very general, and even if you look it up based on this word, a huge amount of things will come up. Therefore, I think it is good to understand from the purpose何のために使うのか
.
summary
- A service is just a process / class
- There is no direct relationship between MVC "models" and services
- "Service" is confusing as a term, so let's understand it from the purpose
Reference information
- PofEAA's Wiki –ServiceLayer
- Service container | Symfony2 Japanese documentation
- Supplementary information about models or service layers-noop days