Laravel Repository 模式

Repository 模式

爲了保持代碼的整潔性和可讀性,使用Repository Pattern 是很是有用的。事實上,咱們也沒必要僅僅爲了使用這個特別的設計模式去使用Laravel,然而在下面的場景下,咱們將使用OOP的框架Laravel 去展現如何使用repositories 使咱們的Controller層再也不那麼囉嗦、更加解耦和易讀。下面讓咱們更深刻的研究一下。php

不使用 repositories

其實使用Repositories並非必要的,在你的應用中你徹底能夠不使用這個設計模式的前提下完成絕大多數的事情,然而隨着時間的推移你可能把本身陷入一個死角,好比不選擇使用Repositories會使你的應用測試很不容易,(swapping out implementations)具體的實現將會變的很複雜,下面咱們看一個例子。
HousesController.phplaravel

<?php
    class HousesController extends BaseController {
        public function index()
        {
            $houses = House::all();
            return View::make('houses.index',compact('houses'));
        }    
        
        public function create()
        {
            return View::make('houses.create');
        }
        public function show($id)
        {
            $house = House::find($id);
            return View::make('houses.show',compact('house'));
        }
    }

  

這是一個很典型的一段代碼使用Eloquent和數據庫交互,這段代碼工做的很正常,可是controller層對於Eloquent而言將是緊耦合的。在此咱們能夠注入一個repository建立一個解耦類型的代碼版本,這個解耦的版本代碼可使後續程序的具體實現更加簡單。數據庫

使用 repositories

其實完成整個repository模式須要至關多的步驟,可是一旦你完成幾回就會天然而然變成了一種習慣了,下面咱們將詳細介紹每一步。後端

1.建立 Repository 文件夾

首先咱們須要在app文件夾建立本身Repository 文件夾repositories,而後文件夾的每個文件都要設置相應的命名空間。設計模式

2: 建立相應的 Interface

第二步建立對應的接口,其決定着咱們的repository類必需要實現的相關方法,以下例所示,在此再次強調的是命名空間必定要記得加上。
HouseRepositoryInterface.php數組

<?php namespace App\Repositories;

interface HouseRepositoryInterface {
    public function selectAll();
    
    public function find($id);
}

  

3:建立對應的 Repository

如今咱們能夠建立咱們repository類 來給咱們幹活了,在這個類文件中咱們能夠把咱們的絕大多數的數據庫查詢都放進去,不論多麼複雜。以下面的例子
DbHouseRepository.phpapp

<?php namespace App\Repositories;

use House;

class DbHouseRepository implements HouseRepositoryInterface {
    
    public function selectAll()
    {
        return House::all();
    }

    public function find($id)
    {
        return House::find($id);
    }
}

  

 

4:建立後端服務提供

首先你須要理解所謂服務提供,請參考手冊服務提供者
BackendServiceProvider.php框架

<?php namespace App\Repositories;

use IlluminateSupportSeriveProvider;

class BackSerivePrivider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('App\Repositories\HouseRepositoryInterface', 'App\Repositories\DbHouseRepository');
    }
}

  

 

固然你也能夠新建一個文件夾主要放咱們的provider相關文件。
上面一段代碼主要說的是,當你在controller層使用類型提示HouseRepositoryInterface,咱們知道你將會使用DbHouseRepository.ide

5:更新你的Providers Array

其實在上面的代碼中,咱們已經實現了一個依賴注入,但若是咱們要使用在此咱們是須要手動去寫的,爲了更爲方面,咱們須要增長這個providers 到app/config/app.php 中的 providers數組裏面,只須要在最後加上App\Repositories\BackendServiceProvider::class,測試

6:最後使用依賴注入更新你的controller

當咱們完成上面的那些內容以後,咱們在Controller只須要簡單的調用方法代替以前的複雜的數據庫調用,以下面內容:
HousesController.php

<?php 

use App\repositories\HouseRepositoryInterface;

class HousesController extends BaseController {
    protected $house;
    
    public function __construct(HouseRepositoryInterface $house)
    {
        $this->house = $house;
    }


    public function index()
    {
        $houses = $this->house->selectAll();

        return View::make('houses.index', compact('houses'));
        
    }


    public function create()
    {
        return View::make('houses.create');
    }


    public function show($id)
    {
        $house = $this->house->find($id);
        
        return View::make('houses.show', compact('house'));

    }
}

  

  這樣 整個模式的轉換就完成了

相關文章
相關標籤/搜索