SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Arnab Mitra
 
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Microservices Docker Kubernetes Istio Kanban DevOps SREMicroservices Docker Kubernetes Istio Kanban DevOps SRE
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Araf Karsh Hamid
 
Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014
JWORKS powered by Ordina
 
[Webinar]: Working with Reactive Spring
[Webinar]: Working with Reactive Spring[Webinar]: Working with Reactive Spring
[Webinar]: Working with Reactive Spring
Knoldus Inc.
 
Clean architecture
Clean architectureClean architecture
Clean architecture
Lieven Doclo
 
CI/CD Overview
CI/CD OverviewCI/CD Overview
CI/CD Overview
An Nguyen
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring Boot
Kashif Ali Siddiqui
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
Tony Tam
 
Clean Architecture
Clean ArchitectureClean Architecture
Clean Architecture
Zahra Heydari
 
Introduction to CI/CD
Introduction to CI/CDIntroduction to CI/CD
Introduction to CI/CD
Steve Mactaggart
 
Getting Started with Azure Artifacts
Getting Started with Azure ArtifactsGetting Started with Azure Artifacts
Getting Started with Azure Artifacts
Callon Campbell
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Knoldus Inc.
 
Kubernetes #1 intro
Kubernetes #1   introKubernetes #1   intro
Kubernetes #1 intro
Terry Cho
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Dvir Volk
 
Azure DevOps
Azure DevOpsAzure DevOps
Azure DevOps
Juan Fabian
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
Remo Jansen
 
API for Beginners
API for BeginnersAPI for Beginners
API for Beginners
Gustavo De Vita
 
Stream processing using Kafka
Stream processing using KafkaStream processing using Kafka
Stream processing using Kafka
Knoldus Inc.
 
Clean architecture
Clean architectureClean architecture
Clean architecture
.NET Crowd
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Arnab Mitra
 
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Microservices Docker Kubernetes Istio Kanban DevOps SREMicroservices Docker Kubernetes Istio Kanban DevOps SRE
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Araf Karsh Hamid
 
Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014
JWORKS powered by Ordina
 
[Webinar]: Working with Reactive Spring
[Webinar]: Working with Reactive Spring[Webinar]: Working with Reactive Spring
[Webinar]: Working with Reactive Spring
Knoldus Inc.
 
Clean architecture
Clean architectureClean architecture
Clean architecture
Lieven Doclo
 
CI/CD Overview
CI/CD OverviewCI/CD Overview
CI/CD Overview
An Nguyen
 
Understanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring BootUnderstanding MicroSERVICE Architecture with Java & Spring Boot
Understanding MicroSERVICE Architecture with Java & Spring Boot
Kashif Ali Siddiqui
 
Introducing Swagger
Introducing SwaggerIntroducing Swagger
Introducing Swagger
Tony Tam
 
Getting Started with Azure Artifacts
Getting Started with Azure ArtifactsGetting Started with Azure Artifacts
Getting Started with Azure Artifacts
Callon Campbell
 
Kubernetes #1 intro
Kubernetes #1   introKubernetes #1   intro
Kubernetes #1 intro
Terry Cho
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Dvir Volk
 
Introducing type script
Introducing type scriptIntroducing type script
Introducing type script
Remo Jansen
 
Stream processing using Kafka
Stream processing using KafkaStream processing using Kafka
Stream processing using Kafka
Knoldus Inc.
 
Clean architecture
Clean architectureClean architecture
Clean architecture
.NET Crowd
 

Similar to Repository design pattern in laravel (20)

Best practices tekx
Best practices tekxBest practices tekx
Best practices tekx
Lorna Mitchell
 
Java 8 Overview
Java 8 OverviewJava 8 Overview
Java 8 Overview
Nicola Pedot
 
Why Laravel?
Why Laravel?Why Laravel?
Why Laravel?
Jonathan Goode
 
Make your application expressive
Make your application expressiveMake your application expressive
Make your application expressive
Christian Varela
 
Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2 Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2
3camp
 
Rupicon 2014 Action pack
Rupicon 2014 Action packRupicon 2014 Action pack
Rupicon 2014 Action pack
rupicon
 
Features java9
Features java9Features java9
Features java9
srmohan06
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
Balint Erdi
 
C# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy today
Afonso Macedo
 
Advanced Interfaces and Repositories in Laravel
Advanced Interfaces and Repositories in LaravelAdvanced Interfaces and Repositories in Laravel
Advanced Interfaces and Repositories in Laravel
Jonathan Behr
 
Content Staging in Drupal 8
Content Staging in Drupal 8Content Staging in Drupal 8
Content Staging in Drupal 8
Dick Olsson
 
What is DDD and how could it help you
What is DDD and how could it help youWhat is DDD and how could it help you
What is DDD and how could it help you
Luis Henrique Mulinari
 
Intro to Rails 4
Intro to Rails 4Intro to Rails 4
Intro to Rails 4
Kartik Sahoo
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
Knoldus Inc.
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
Beyond MVC: from Model to Domain
Beyond MVC: from Model to DomainBeyond MVC: from Model to Domain
Beyond MVC: from Model to Domain
Jeremy Cook
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
C4Media
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
Make your application expressive
Make your application expressiveMake your application expressive
Make your application expressive
Christian Varela
 
Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2 Osiąganie mądrej architektury z Symfony2
Osiąganie mądrej architektury z Symfony2
3camp
 
Rupicon 2014 Action pack
Rupicon 2014 Action packRupicon 2014 Action pack
Rupicon 2014 Action pack
rupicon
 
Features java9
Features java9Features java9
Features java9
srmohan06
 
C# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy today
Afonso Macedo
 
Advanced Interfaces and Repositories in Laravel
Advanced Interfaces and Repositories in LaravelAdvanced Interfaces and Repositories in Laravel
Advanced Interfaces and Repositories in Laravel
Jonathan Behr
 
Content Staging in Drupal 8
Content Staging in Drupal 8Content Staging in Drupal 8
Content Staging in Drupal 8
Dick Olsson
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
Plataformatec
 
React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)React Native +Redux + ES6 (Updated)
React Native +Redux + ES6 (Updated)
Chiew Carol
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
Knoldus Inc.
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
Beyond MVC: from Model to Domain
Beyond MVC: from Model to DomainBeyond MVC: from Model to Domain
Beyond MVC: from Model to Domain
Jeremy Cook
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
C4Media
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
Ad

Repository design pattern in laravel

  • 1. Repository Design Pattern in Laravel Samir Poudel
  • 2. 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
  • 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 always awesome to use. So why to use these design patterns ?
  • 6. 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
  • 8. 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.
  • 9. What’s a repository class CommonRepository { public function all() { return Contacts::all(); } }
  • 10. Repository Controller class CommonController extends 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 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' ),
  • 13. 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(); } }
  • 14. What if we change DB ? Switching to any new database or any changes are simple now. $this->app->bind( ‘RepositoryStorageCommonCommonRepository', ‘RepositoryStorageCommonMongoCommonRepository' );
  • 15. I don’t have more to say ;) Ways to connect with me @samirpdl www.samirpdl.com.np [email protected]