Repository Design
Pattern in Laravel
Samir Poudel
The elements of this language are entities called patterns. Each pattern describes
a problem that occurs over and over again in our environment, and then
describes the core of the solution to that problem, in such a way that you can use
this solution a million times over, without ever doing it the same way twice.
— Christopher Alexander
Design Patterns
1. Builder
2. Chain of responsibility
3. Command
4. Facade
5. Factory
6. Iterator
7. Mediator
8. Observer
9. Presenter
10.Repository
11.Singleton
12.Strategy
LARAVEL is always awesome to
use. So why to use these design
patterns ?
Repository Design Pattern
class CommonController extends
BaseController
{
public function all()
{
$contacts = Contacts:all();
return
View::make(‘contacts.all’,
compact($contacts));
}
}
So it’s the way right ?
Common Controller
1. Tied to eloquent.
2. Not easily testable.
3. Maintenance is tough.
4. Bugs.
5. bla bla bla bla
Now what ??
Repositories
1. Repository design pattern is actually simple. Its just the addition of another
layer into your system. The Service Layer.
2. By this you add another layer to communicate with your database and
other thing instead of controller.
3. No need to write multiple codes.
4. Easily changeable DB.
5. Less headache.
What’s a repository
class CommonRepository
{
public function all()
{
return Contacts::all();
}
}
Repository Controller
class CommonController extends BaseController
{
public function all()
{
$contactsRepo = new ContactRepository();
$contacts = $contactsRepo->all();
return View::make(‘contacts.list’, compact($contacts));
}
}
Project Structure
- app
-- lib
--- Repository
---- Storage
----- Common
Interface Class : CommonRepository.php
Eloquent Class Implemented from interface : EloquentCommonRepository.php
Creating the service provider to bind two repositories together
<?php namespace RepositoryStorage;
use IlluminateSupportServiceProvider;
class StorageServiceProvider extends ServiceProvider
{
public function register() {
$this->app->bind( RepositoryStorageUserCommonRepository,
RepositoryStorageCommonEloquentCommonRepository );
}
}
Finally in the providers listing you need to add it
'providers' => array( // -- RepositoryStorageStorageServiceProvider' ),
Implementation
<?php
use RepositoryStorageCommonCommonRepository as Common;
class CommonController extends BaseController {
public function __construct(Common $common)
{
$this->common = $common;
}
public function index()
{
return $this->common->function();
}
}
What if we change DB ?
Switching to any new database or any changes are simple now.
$this->app->bind( ‘RepositoryStorageCommonCommonRepository',
‘RepositoryStorageCommonMongoCommonRepository' );
I don’t have more to say ;)
Ways to connect with me
@samirpdl
www.samirpdl.com.np
samir@samirpdl.com.np

Repository design pattern in laravel

  • 1.
    Repository Design Pattern inLaravel Samir Poudel
  • 2.
    The elements ofthis language are entities called patterns. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. — Christopher Alexander
  • 3.
    Design Patterns 1. Builder 2.Chain of responsibility 3. Command 4. Facade 5. Factory 6. Iterator 7. Mediator 8. Observer 9. Presenter 10.Repository 11.Singleton 12.Strategy
  • 4.
    LARAVEL is alwaysawesome to use. So why to use these design patterns ?
  • 5.
  • 6.
    class CommonController extends BaseController { publicfunction all() { $contacts = Contacts:all(); return View::make(‘contacts.all’, compact($contacts)); } } So it’s the way right ? Common Controller 1. Tied to eloquent. 2. Not easily testable. 3. Maintenance is tough. 4. Bugs. 5. bla bla bla bla
  • 7.
  • 8.
    Repositories 1. Repository designpattern is actually simple. Its just the addition of another layer into your system. The Service Layer. 2. By this you add another layer to communicate with your database and other thing instead of controller. 3. No need to write multiple codes. 4. Easily changeable DB. 5. Less headache.
  • 9.
    What’s a repository classCommonRepository { public function all() { return Contacts::all(); } }
  • 10.
    Repository Controller class CommonControllerextends BaseController { public function all() { $contactsRepo = new ContactRepository(); $contacts = $contactsRepo->all(); return View::make(‘contacts.list’, compact($contacts)); } }
  • 11.
    Project Structure - app --lib --- Repository ---- Storage ----- Common Interface Class : CommonRepository.php Eloquent Class Implemented from interface : EloquentCommonRepository.php
  • 12.
    Creating the serviceprovider to bind two repositories together <?php namespace RepositoryStorage; use IlluminateSupportServiceProvider; class StorageServiceProvider extends ServiceProvider { public function register() { $this->app->bind( RepositoryStorageUserCommonRepository, RepositoryStorageCommonEloquentCommonRepository ); } } Finally in the providers listing you need to add it 'providers' => array( // -- RepositoryStorageStorageServiceProvider' ),
  • 13.
    Implementation <?php use RepositoryStorageCommonCommonRepository asCommon; class CommonController extends BaseController { public function __construct(Common $common) { $this->common = $common; } public function index() { return $this->common->function(); } }
  • 14.
    What if wechange DB ? Switching to any new database or any changes are simple now. $this->app->bind( ‘RepositoryStorageCommonCommonRepository', ‘RepositoryStorageCommonMongoCommonRepository' );
  • 15.
    I don’t havemore to say ;) Ways to connect with me @samirpdl www.samirpdl.com.np samir@samirpdl.com.np