Please tell me about the role of services in MVC models?

Asked 2 years ago, Updated 2 years ago, 160 views

What is the role of the service?

I learned MVC from CakePHP, but I don't know what role CakePHP has because it doesn't have any services.

The controller extracts the necessary actions for user requests and
Views are displayed to communicate results, etc.,
I understand that the model summarizes the actions required by the controller.
(Is it wrong?)

Recently, I learned that there is a concept of service. Is this further divided into models?Or are you talking about the model?

I would appreciate it if you could tell me about the service in an easy-to-understand way.

cakephp mvc

2022-09-30 20:32

3 Answers

I think other people's answers will help you understand one thing in one framework called CakePHP, but I will take a step back and answer what MVC and service are generally related to each other.

The question says about MVC:

The controller extracts the necessary actions for user requests and
Views are displayed to communicate results, etc.,
I understand that the model summarizes the actions required by the controller.

This classification is not so wrong.In fact, however, there are many different types of "processing required by the controller."For example,

  • Save information to DB/Get information from DB
  • Send mail
  • Create a thumbnail of the uploaded image file

is available.You have no idea what these models are, what business logic is, and what services are.Let's organize our words.

Generally speaking, "model" refers to an easy-to-understand representation of how it works and how it works.If it's a business system, it can be the structure of information, rules on how to create information, and procedures.

What is the CakePHP Model?

In the case of CakePHP's Model, it automatically reads the schema of the database and makes it easy to handle records in that table, so at first glance, the role of the Model looks like a "code storage space for DB input and output."However, the original purpose of ActiveRecord like this is to allow application codes to handle the form of information in a uniform format.

In other MVC frameworks, the part named Model is thus given the purpose of representing the form of information.The processing that accompanies this form of information (input/output of information, checking the integrity of the form, and some variation/synthesis of the form) will be described in the Model.

What are the rules and procedures?

Some of the first examples were rules and procedures.Are these attached to the model, or the form of information?

A common example is the procedure (procedure) for a member to purchase an item.We check the purchase limit for each member and then check the inventory.In some steps, you must cross-cut across multiple pieces of information.

There is also a way to cut out what was described in the model in some other way based on the standard of handling multiple things, but more correctly, step = make the parts representing use cases independent.This is one form of how to use the service described below.

Service refers to the processing object.

For example, if an application hashes and records a user's password, it may have made the hashing process independent of one class.This class is the PasswordEncoder service.In short, the class that provides processing, whether it's a library or anything, is a service.This PasswordEncoder service can also be used internally by the Model or by the Controller for other reasons when storing user information.

Using Services

As for how to use the service, as was the case with PasswordEncoder earlier, there are a few things that I can think of.

  • Use the common actions described in the controller to summarize them from some point of view
  • Use to summarize the common actions described in the Model from some perspective
  • Use to summarize common actions needed for templates in some way

For controller common processing, you can create a controller base class and create a common method there, but this is not a very good method.Instead of decreasing code from individual controllers, complexity can be concentrated on the base class side.

A framework that can handle "services" directly can be used in common procedures such as simply creating classes for individual processing and instructing the framework to be available where necessary (e.g., Symfony's service container).In the case of CakePHP, there are individual mechanisms such as helper depending on where you use it, but beyond that, if you can use your own class, you will be able to expand your range of responses.

Is the service Model?

As in the previous section, any part of MVC can be processed into a service.Because services are just things that provide processing.In this sense, services are basically unrelated to the structure of a Model or Controller.

Service Layer (Use Case Layer)

  • Put it between the controller and the model and use it like a layer of operation for the model.

There is also a way to use it.This is not to be used as a part, but to add structure to the architecture (how it is made) called MVC.In technical terms, it is called a service layer or use case layer.This way, you can improve the outlook for your production code.

The service described in the term "microservices" which is a hot topic these days refers to a larger unit than "one class" described here."For example, ""membership management services"" within a single company can be interpreted as a single service."

The word "service" is very versatile, and even if you look at it based on this word, you'll find a lot of things.Therefore, I think it would be good to understand the purpose of .

  • Services are just processing/classes
  • MVC "model" and services are not directly related
  • Understand the term "service" from a purposeful perspective


2022-09-30 20:32

To simply answer the question, CakePHP has Component in the Service layer.

I came across the following entry written by CakePHP core developer Jose when I had the same question.

There is no definition that clearly separates MVC model from service layer, but CakePHP is refreshing to implement as Component.
Roughly speaking, it looks like the following.

  • Model is simply used as an OR mapper for individual tables
  • The methods implemented in the Model include custom viewfinder for individual models (use it as a shortcut to avoid specifying Where one by one).
  • like FindLatest()
  • Component implements high-level processing such as sequential calling of multiple models and provides an API to the controller.
  • (Summarizes DB-unlimited processing such as invoking table models, WEBAPI, and e-mail from methods such as RegisterUser)

However, this can also be disproved.

  • Model can also be created in a way that does not correspond to the database table. (useTable=false)
  • Model can also handle other models in a service-like manner.
  • It is often said that it is wrong to assume that the Model can only be an OR mapper.

After all, it's only discretion, but implementing it as a Component makes it easier to handle controller relationships such as WebAPI and sessions, and I think it's easy to understand that the Model is limited to DAO to the table.

In addition, in CakePHP3, the Model was separated into Table and Entity classes.
This will make it easier to separate the DAO portion from the logical processing under the Model.


2022-09-30 20:32

In MVC in web apps, controller/action usually has a specific URL pattern and model is DB table.

Some requirements may require complex model operations, such as actions to bulk updates and split searches, but writing them to a controller or model is not well-behaved, so implement service methods by dividing them into data-valid model operations.

I understand that the model summarizes the actions required by the controller.

If that's the case, I think the service is the logic side that divides the model into the logic part that corresponds to simple SQL and the logic part that combines SQL.

Non-MVC architectures are often referred to as business logic.


2022-09-30 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.