鍍金池/ 問答/PHP/ 關(guān)于在項(xiàng)目中增加repository的迷惑, 請大家?guī)蛶兔?

關(guān)于在項(xiàng)目中增加repository的迷惑, 請大家?guī)蛶兔?

本人入行尚淺,在工作中發(fā)現(xiàn)controller里面要寫的代碼量太亂了,又是驗(yàn)證又是邏輯的. 所以在網(wǎng)上查了下,發(fā)現(xiàn)可以加個(gè)repository層. 查看了下一般的做法,貌似是創(chuàng)建一個(gè)interface或者abstract.然后去繼承或者實(shí)現(xiàn)它們.里面定義find(),first(),where(),get(),delete(),update()等常用的方法. 在這里想像大神們請教下,如果單一的方法解決不了怎么辦呢? 就是說查詢條件很多,可能同時(shí)有where(),whereIn(),或者between,那么這些簡單的操作就無法完成了, 如果實(shí)現(xiàn)所有的功能,那不就等于是把eloquent重寫了一遍么 ? 或者是在repository中的子類專門定義一個(gè)方法來完成單獨(dú)的一個(gè)操作 ? 請指點(diǎn),謝謝.

回答
編輯回答
風(fēng)清揚(yáng)

你說的只是基礎(chǔ)的
比如:

public function getAgeLargerThan($age)
{
    return $this->user
        ->where('age', '>', $age)
        ->orderBy('age')
        ->get();
}

可以看一下這篇文章:如何使用 Repository 模式?

2018年7月7日 06:39
編輯回答
背叛者

先解釋一下我對repository的理解
repository的用意就是在model和controller中間加一個(gè)“中間層”。假如你現(xiàn)在有一個(gè)Model叫CDEloquent,用于在關(guān)系型數(shù)據(jù)庫中存儲(chǔ)唱片信息,它繼承了CD Interface
CD Interface

namespace App\Interface
interface CD
{
    public function getName();
    public function getAuthor();
}

CDEloquent

namespace App\Eloquent
use App\Interface\CD
class CDEloquent extends CD
{
    public function getName() 
    {
        //在關(guān)系型數(shù)據(jù)庫中的操作
    }
 
    public function getAuthor() 
    {
        //在關(guān)系型數(shù)據(jù)庫中的操作
    }
}

然后你就會(huì)在Controller中取調(diào)用CDEloquent中的方法。
后來因?yàn)槟承┰蚰愕腸d model不使用關(guān)系型數(shù)據(jù)庫了而是使用MongoDB。在這時(shí)你的Controller里面可能已經(jīng)調(diào)用到CDEloquent中的方法了,有可能你需要修改大量的Controller中的代碼。如果當(dāng)時(shí)我們定義一個(gè)repository

namespace App\Repository
use App\Eloquent\CDEloquent
class CDRepository
{
    protected $model
    public function setModel(CDEloquent $cd)
    {
        $this->model = $cd;
    }
    public function getName()
    {
        return $this->model->getName();
    } 
    public function getAuthor()
    {
        return $this->model->getAuthor;
    }
}

然后我們在controller中調(diào)用CDRepository而不是CDEloquent。等到我們要將model從CDEloquent換到使用mongodb存儲(chǔ)時(shí)只需要先定義一個(gè)CDMongo

namespace
App\Mongo
use App\Interface\CD
class CDMongo extends CD
{
    public function getName()
    {
        \\mongodb中的操作
    }
    public function getAuthor()
    {
        \\mongodb中的操作
    }
}

然后將respository中的setModel改為

public function setModel(CDMongo $cd)
{
        $this->model = $cd;
}

這樣我的controller中的代碼不需要改任何地方。
因此respository的作用是為controller層提供一個(gè)統(tǒng)一的接口,然后具體的實(shí)現(xiàn)是由respository中的model來實(shí)現(xiàn)的。respository只是一個(gè)邏輯抽象層。它將controller和model之間的耦合性降低了。
如果你的業(yè)務(wù)只是用關(guān)系型數(shù)據(jù)庫存放并且不會(huì)更改的話,就像你說的,完全沒有必要加respository這一層。只有當(dāng)model的類型有可能改變的時(shí)候才會(huì)加。

如果使用了respository。你可以把respository看成是以前model層的代理,在controller中使用model的地方改使用respository;

至于你說controller的功能太亂了又是驗(yàn)證又是邏輯的,我的想法是:
以前我們開發(fā)講究MVC。但是現(xiàn)在前后端分離是主流,對于后臺(tái)開發(fā)者就沒有view層了,只剩下MV;
在MV之間可以加一個(gè)respository,上面已經(jīng)說了就不再說了。其實(shí)我們也可以把傳統(tǒng)的controller拆為兩個(gè)層:controller和logic。controller只負(fù)責(zé)接受請求,驗(yàn)證數(shù)據(jù),發(fā)送回復(fù)。具體的業(yè)務(wù)邏輯放到logic中去。這樣無論是代碼的清晰度還是業(yè)務(wù)邏輯都會(huì)很清晰。
以上是個(gè)人的一些觀點(diǎn),如有錯(cuò)誤還請見諒

2018年4月16日 19:12