Home » Php » laravel – Dependency inversion issue in php. (Repository Pattern)

laravel – Dependency inversion issue in php. (Repository Pattern)

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am implementing a repository pattern in Laravel, and it seems to be very tedious. For example, let’s say I have products then I have to create a ProductRepository interface then a ProductRepository class that implements that interface, now I have some very generic methods on the ProductRepository like:

  • retrieveAll
  • store
  • update
  • delete

And now I have to do the same thing for ingredients. It would be nice if I could simply create a ModelRepository interface with all those generic methods and implement it by passing a generic data type (namely the model), something similar to Java Generics:

<?php
interface ModelRepositoryInterface<T> {
    function retrieveAll(): Collection<T>;
    function store(T $item);
    function update(int $id, T $data);
    function delete(int $id);
}

But since php doesn’t support generics how can I achieve this simplicity?

How to&Answers:

You can create a RepositoryServiceProvider to bind your repository interfaces to actual classes.

You can create a abstract Repository class with retrieveAll, store, update, delete and extend your Repositories and implement the interface. I have included below example with magic functions to be able to eloquent methods if I don’t have any customization.

The below is not tested but its just to get the idea.

<?php


namespace App\Repositories;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

abstract class AbstractRepository implements RepositoryInterface
{
    /**
     * @var Builder|Model
     */
    protected $model;
    /**
     * @return mixed
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * @param array $columns
     * @return \Illuminate\Database\Eloquent\Collection|Model[]
     */
    public function all($columns = ['*'])
    {
        return $this->model->all($columns);
    }

    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        return $this->model->{$name}($arguments);
    }
}

OrderRepository

<?php

namespace App\Repositories;

use App\Models\Order;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;

class OrderRepository extends AbstractRepository implements OrderRepositoryInterface
{
    /**
     * OrderRepository constructor.
     * @param Order $model
     */
    public function __construct(Order $model)
    {
        $this->model = $model;
    }

    public function countPaid(): int
    {
        return $this->model->paid()->count();
    }

    /**
     * @return int
     */
    public function countReady(): int
    {
        return $this->model->ready()->count();
    }

    /**
     * @return int
     */
    public function countCancelled(): int
    {
        return $this->model->cancelled()->count();
    }

}

OrderRepositoryInterface

<?php


namespace App\Repositories;


interface OrderRepositoryInterface
{

}

RepositoryServiceProvider

<?php

namespace App\Providers;

use App\Repositories\OrderRepository;
use App\Repositories\OrderRepositoryInterface;
use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(OrderRepositoryInterface::class, OrderRepository::class);
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

RepositoryInterface

<?php


namespace App\Repositories;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

interface RepositoryInterface
{
    function retrieveAll(): Collection;
    function store(Model $item);
    function update(int $id, Model $data);
    function delete(int $id);
}