为何你应该使用 Repository

原文来自http://vegibit.com/laravel-repository-pattern/php

Repository 模式

为了保持代码的整洁性和可读性,使用Repository Pattern 是很是有用的。事实上,咱们也没必要仅仅为了使用这个特别的设计模式去使用Laravel,然而在下面的场景下,咱们将使用OOP的框架Laravel 去展现如何使用repositories 使咱们的Controller层再也不那么啰嗦、更加解耦和易读。下面让咱们更深刻的研究一下。laravel

不使用 repositories

其实使用Repositories并非必要的,在你的应用中你彻底能够不使用这个设计模式的前提下完成绝大多数的事情,然而随着时间的推移你可能把本身陷入一个死角,好比不选择使用Repositories会使你的应用测试很不容易,具体的实现将会变的很复杂,下面咱们看一个例子。
HousesController.php数据库

<?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.phpapp

<?php namespace App\Repositories;

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

3:建立对应的 Repository

如今咱们能够建立咱们repository类 来给咱们干活了,在这个类文件中咱们能够把咱们的绝大多数的数据库查询都放进去,不论多么复杂。以下面的例子
DbHouseRepository.php框架

<?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.phpide

<?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.测试

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 {

    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'));

    }
}

这样整个的流程就完成了。翻译的不太好,请你们见谅,作了一些改动。

相关文章
相关标签/搜索