本人入行尚淺,在工作中發(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),謝謝.
你說的只是基礎(chǔ)的
比如:
public function getAgeLargerThan($age)
{
return $this->user
->where('age', '>', $age)
->orderBy('age')
->get();
}
可以看一下這篇文章:如何使用 Repository 模式?
先解釋一下我對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ò)誤還請見諒
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。