Necessity analysis and cases of service layer

Time:2021-11-28

preface

Previously, I saw such a question as “is it necessary to separate the service layer in the yii2 framework?”. I also got the answer from others’ answers, but I think it still needs a living corn to have more clarity and strong persuasion. If you are interested in my actual combat experience, continue to look down. If you like, you can also click recommend and collect. Before I give you the corn, let me talk about what service is? Does it work? Lest anyone else be confused.
1. What is service?
In OO oriented systems, service is biz manager, and in process oriented systems, service is ts script.
2. What is the function of service?
The function of the service layer is to encapsulate these complex business logic that requires the participation of multiple models. There is no direct dependency between these models, but to cooperate to complete the logic in the service layer. The first purpose of the service layer is actually to decouple the model layer.

requirement analysis

1. Establish a service layer in yii2 framework to deal with common and complex business logic.

design sketch

1. Create a service layer under common.
Necessity analysis and cases of service layer

2. Some common data processing logic (the main data processing is written here).
Necessity analysis and cases of service layer

code analysis

1. Write a cluesbranchservice.php file under commonservice. Cluesbranchservice class inherits the main models class chance of this module. All public business logic about chance is written in this file.

namespace common\service;

use Yii;
use api\modules\v1\models\Sales;
use api\modules\v1\chance\models\Chance;

/**
 *// public data processing logic of subordinate clues
 */
class CluesBranchService extends Chance
{   
    //List of leads for subordinates
    public static function getIndex()
    {    
        $SalesModel = new Sales(); 
        $uids = $SalesModel->sevenChild(Yii::$app->user->id);
        if(count($uids)){
            $query = Chance::find()->where(['in','owner_id',$uids]);
        }else{
            $query = Chance::find()->where(['owner_id'=>'-1']);
        }
       return $query;
    }
}

2. Called in controllers.

use common\service\CluesBranchService;

$query = CluesBranchService::getIndex();

Note: what is returned here is $query, not the query result. Those who have used yii2 know that the list paging uses activedataprovider. There is no need to find the result. In order to unify, we directly return $query here. If there is a special need to add where, andwhere or obtain data results, you can do this: $query – > where ([‘condition’])$ query->all()。

Analysis and summary

The above is the implementation of a service layer with relatively simple business logic. Some people may wonder whether the service layer should be separated or not?
To sum up, if you need more than one model for a certain business logic, put it in the service layer. If it is only the business of this model and has nothing to do with other models, just put it in the model.
If your system is small, the business logic is super simple, and there is no need for long-term evolution iteration, you can write as you like.

Relevant information

In the yii2 framework, is it necessary to separate the service layerhttps://segmentfault.com/q/1010000003849810