The necessity of service layer analysis and case

Source: Internet
Author: User
Tags yii
This is a creation in Article, where the information may have evolved or changed.

Preface

Before, I read a question like "YII2 framework, is it necessary to separate the service layer?" "From the other people's answers, they also harvested the answer, but I think there is still a living chestnut to have a more clear and powerful persuasion." If you are interested in my actual combat experience continue to look down, like can also click on the recommendation and collection. Before I lift the chestnut, I'll tell you what the service is. What's the effect? Lest anyone else be confused.
1. What is the service?
In Oo-oriented systems, the service is the Biz Manager, and the service is a TS script in a process-oriented system.
2. What role does the service have?
The service layer's role is to encapsulate these complex business logic that requires multiple model participation, which no longer has a direct dependency, but rather a collaborative logic within the service layer. The first purpose of a service layer is to decouple the model layer.

Demand analysis

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

1. Establish a service layer under the common.

2, part of the Public Data processing logic (the main data processing is written here).

Code Analysis

1, write a cluesbranchservice.php file under Commonservice, Cluesbranchservice class inherits the main models class chance of this module. All the 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;/** * //下属的线索公共数据处理逻辑 */class CluesBranchService extends Chance{       //下属的线索列表    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. Call in controllers.

use common\service\CluesBranchService;$query = CluesBranchService::getIndex();

Note: Here is the return of $query, rather than the results of the query, used YII2 all know the list implementation of the page is Activedataprovider, do not need to find the results, in order to unify so here directly return to $query. If there is a special need to add where, andwhere, or obtain data results can be such $query->where ([' condition ']); $query->all ().

Analysis Summary

The above is a simple business logic implementation of the service layer, see here there may be some doubts, in the end should not be separated service layer?
Simple rough Summary, if you have a business logic, need to use multiple model, put into the service layer, if only this model of their own things, and other model has nothing to do with the model inside.
If your system is small, the business logic is super simple, and there is no need for long-term evolutionary iterations, you can do whatever you like.

Related information

Yii2 Framework, is it necessary to separate the service layer? : https://segmentfault.com/q/1010000003849810

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.