Repository pattern with dependency injection

This article focuses to share about

1. What is Repository pattern and its advantages, How to implement it with Laravel

2. What is Dependency injection and How to do it

3.Why abstraction (Interface) is used in order to do Dependency injection. Is it possible to do Injection without Interface

 

Repository pattern and its advantages?

Repository pattern is one of the useful patterns which provides the following advantages

1.it makes a layer between the application logic and the data access layer.

2. It allows programmers to apply SOLID principal easily.

 

Dependency Injection ?

The dependency injection is a pattern which applies Dependency  Inversion Principals, it is passing dependency to other objects.

The injection can be done by a constructor or by a set method.

The important advantage is decoupling the code which provides flexibilities for unit testing.

 

Is it possible to do Injection without Interface?

Yes of course, without interface it is possible, but it is not best practice as its violating SOLID principals.The classes should depend on abstractions, not on a concrete class that makes to implement the unit testing complicated.

 

Implementing Repository pattern with Laravel

  • Creating  Interfaces
  • Creating Repositories
  • Registering repositories with IOC container
  • Implementing dependency injection

 

Creating an interface 

This is an abstraction class. Any classes can be used to implement it.

Under folder app/Interfaces ,ArticleReportInterface


<?php
namespace App\Interfaces\Article;


Interface ArticleReportInterface {


    /**
     * Displaying articles report
     *
     * @param String $startDate
     * @param String $endDate
     * @return JSON
     */
    public function showArticleReport(String $startDate,String $endDate);

}

Creating the repository
This is the concrete class of an abstraction, app/Repositories,This will call to database access and other business logic which implemented under service

<?php 
namespace App\Repositories\Articles;

 use App\Interfaces\Article\ArticleReportInterface;
 class ArticlesRepositories implements ArticleReportInterface { 
/** 
* This is the place where the complex logic will be added 
* 
* @param String $startDate
* @param String $endDate 
* @return mixed
* @todo later logic will be implemented 
*/ 
public function showArticleReport(String $startDate, String $endDate) { 
$sampleOutput = ['sample outout' => 'sample output'];

        return $sampleOutput;
    }
}

Registering repositories with IOC container

Create a file is called, RepositoryServiceProvider under providers folder.Then  bind the interface and repsository into register method.

<?php namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 
use App\Interfaces\Article\ArticleReportInterface;
use App\Repositories\Articles\ArticlesRepositories; 


class RepositoryServiceProvider extends serviceProvider{ 
public function boot() { } 
public function register(){
 $this->app->bind('App\Interfaces\Article\ArticleReportInterface', 'App\Repositories\Articles\ArticlesRepositories');
    }


}

a Sample of implementing Dependency Injection


<?php 
namespace App\Console\Commands; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log; 
use App\Interfaces\Article\ArticleReportInterface;

 class ArticleReport extends Command {
 protected $articleReportRepository; 
/** 
* The name and signature of the console command. 
* 
* @var string 
*/ 
protected $signature = 'article-report {fromDate} {toDate}'; 
/** 
* The console command description. 
* 
* @var string 
*/ 
protected $description = 'Article report generator';
/** 
* Create a new command instance. 
*
* @param $articleReportRepository 
* @return void 
*/ 
public function __construct(ArticleReportInterface $articleReportRepository) { //it is injected without creating constructor
parent::__construct(); 
$this->articleReportRepository =$articleReportRepository;
}
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       $this->articleReportRepository->showArticleReport($this->argument('fromDate'),$this->argument('endDate'));

    }
}

 

In conclusion, the repository pattern with Dependency Injection makes decoupling the code as well as separate the logic.

Source url ,dp-with-repository