Repository

Repository 模式

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

不使用 repositories

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

`<?php
class HousesController extends BaseController {
public function index()
{
$houses = House::all();
return View::make(‘houses.index’,compact(‘houses’));
}

 
 
  1. public function create()
  2. {
  3. return View::make('houses.create');
  4. }
  5. public function show($id)
  6. {
  7. $house = House::find($id);
  8. return View::make('houses.show',compact('house'));
  9. }

}`
这是一个很典型的一段代码使用Eloquent和数据库交互,这段代码工作的很正常,但是controller层对于Eloquent而言将是紧耦合的。在此我们可以注入一个repository创建一个解耦类型的代码版本,这个解耦的版本代码可以使后续程序的具体实现更加简单。

使用 repositories 其实完成整个repository模式需要相当多的步骤,但是一旦你完成几次就会自然而然变成了一种习惯了,下面我们将详细介绍每一步。

1. 创建 Repository 文件夹

首先我们需要在app文件夹创建自己Repository文件夹repositories,然后文件夹的每一个文件都要设置相应的命名空间。

2: 创建相应的 Interface类

第二步创建对应的接口,其决定着我们的repository类必须要实现的相关方法,如下例所示,在此再次强调的是命名空间一定要记得加上。 HouseRepositoryInterface.php

 
 
  1. <?php namespace App\Repositories;
  2. interface HouseRepositoryInterface {
  3. public function selectAll();
  4. public function find($id);
  5. }

3: 创建对应的 Repository类

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

 
 
  1. <?php namespace App\Repositories;
  2. use House;
  3. class DbHouseRepository implements HouseRepositoryInterface {
  4. public function selectAll()
  5. {
  6. return House::all();
  7. }
  8. public function find($id)
  9. {
  10. return House::find($id);
  11. }
  12. }

4: 创建后端服务提供

首先你需要理解所谓服务提供,请参考手册服务提供者 BackendServiceProvider.php

<?php namespace App\Repositories;

 
 
  1. use IlluminateSupportSeriveProvider;
  2. class BackSerivePrivider extends ServiceProvider {
  3. public function register()
  4. {
  5. $this->app->bind('App\Repositories\HouseRepositoryInterface', 'App\Repositories\DbHouseRepository');
  6. }
  7. }

当然你也可以新建一个文件夹主要放我们的provider相关文件。 上面一段代码主要说的是,当你在controller层使用类型提示HouseRepositoryInterface, 我们知道你将会使用DbHouseRepository.

5: 更新你的Providers Array

其实在上面的代码中,我们已经实现了一个依赖注入,但如果我们要使用在此我们是需要手动去写的,为了更为方面,我们需要增加这个providers到 app/config/app.php 中的 providers数组里面, 只需要在最后加上App\Repositories\BackendServiceProvider::class,

6:最后使用依赖注入更新你的controller

当我们完成上面的那些内容之后,我们在Controller只需要简单的调用方法代替之前的复杂的数据库调用,如下面内容: HousesController.php

 
 
  1. <?php
  2. use App\repositories\HouseRepositoryInterface;
  3. class HousesController extends BaseController {
  4. public function __construct(HouseRepositoryInterface $house)
  5. {
  6. $this->house = $house;
  7. }
  8. public function index()
  9. {
  10. $houses = $this->house->selectAll();
  11. return View::make('houses.index', compact('houses'));
  12. }
  13. public function create()
  14. {
  15. return View::make('houses.create');
  16. }
  17. public function show($id)
  18. {
  19. $house = $this->house->find($id);
  20. return View::make('houses.show', compact('house'));
  21. }
  22. }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值