How and why we implemented the “Repository Pattern” in our PHP backend

Valerio Barbera

Hi, I’m Valerio, CTO and founder at Inspector. In this article I talk about the Repository Pattern and how we implemented it in our application to solve a scalability problem.

The Repository Pattern is one of the most disccussed patterns due to a lot of conflicts with ORMs. This pattern is often intended as an abstraction layer to interact with the database. Since the ORMs are designed for the same purpose many developers get confused.

We discuss this topic in detail, explaining why and how we implemented it in our backend.

Why use an abstraction layer for data access?

The most obvious reason of why an abstraction layer exists in almost any application, is to drastically reduce code duplication.

ORM is the most known abstraction layer used to easy access and modify data against SQL databases. Laravel has Eloquent, Symfony has Doctrine, etc.

Using an ORM your business logic could be somthing like:

$user = (new User())->find($id);
$user->first_name = "Valerio";
$user->save();

This is just business logic, it doesn’t care about how and where the data are stored, it depends by the internal ORM implementation and configuration. If you are a developer with professional experience, surely you are already using an ORM every day.

They already provide an abstraction layer to have a smart access to data. So, why we should use also the “Repository Pattern”?

In fact, you shouldn’t use it by default contrary to what many developers claim.

Most of the technical articles I have read on the subject talk about Repository Pattern in theory, probably just to push some contents in the Google results for that specific topic. They start from simplistic assumptions that are not reflected in practical experience, so I myself have struggled to understand if it was the right solution for me, or if I could move my effort on more urgent and productive tasks.

If you’ve had the same issue as me, you may find our experience helpful.

Why might we need the “Repository Pattern”?

I would start talking about some bogus problems, in order to clarify immediately when it is “not” necessary to introduce the Repository layer.

Many developers think about the Repository Pattern like an insurance:

“If you need to change XXX in the future you can do it without having to break the whole application”.

In many articles the typical examples refers to:

  • Change the database – but ORMs are already designed for this;
  • Change the ORM – Changing the ORM is such a drastic step that in 99% of cases it happens because you have to change the whole framework you are working with, or even completely change technology. If have a plan for this change the Repository Pattern is the least of your problems. In this case you are probably over engineering your code.

I think this isn’t the right way to think, because with the limited time available and the tight budget, we cannot focus on scenarios that may never occur in the life span of the project. We need to solve problems we are facing now.

The need to an additional layer on top of the default data-access layer provided by the ORM could comes in several scenarios. ORMs interact with databases, but don’t necessarily incapsulate complex statements.

So here is some real-life scenarios where you could need an additional layer on top of the ORM:

  • You may have some complex query that you need to call from different place in your code;
  • You may need to implement custom actions on an entity model that performs some data manipulation statements moving data from/to the database;
  • Embrace new technologies like cache systems on top of your standard connection with the database.

A Repository acts like a collection of domain objects, with powerful querying and data manipulation capabilities. Its main function is to provide collection like (query-enabled) access to domain objects (whether they are gotten from a database is besides the point). Repositories may (and often will) contain ORMs operations themselves.

The more you find that you are using elaborate query logic or custom actions in your ORM, the more you want to start thinking about decoupling that logic into a repository while leaving your ORM to serve their main function, mapping domain objects to the database and vice versa.

Why I decided to add a Repostory layer in Inspector?

Our decision to introduce the “Repository layer” in Inspector was dictated by two of the three reasons mentioned above:

  • We have several custom actions on various models that we want to group in a central place instead of repeating them in different parts of the code;
  • We want to add a cache layer on top of the database to increase performance.

Thanks to the Laravel IoC container, we were able to create a specific Repository layer for each of these problems.

Repository interface

namespace App\Repositories\Contracts;

use App\Models\Organization;

interface OrganizationRepository
{
    public function get($id): Organization;
    public function create(array $attributes): Organization;
    public function update($id, array $attributes): Organization;
    public function updateCurrentBillingConsumption($id, $value = null): Organization;
    public function addBonusTransactions($id, int $qty): Organization;
    public function lock($id): Organization;
    public function unlock($id): Organization;
    public function delete($id);
}

Eloquent repository

namespace App\Repositories\Eloquent;


use App\Events\OrganizationLocked;
use App\Events\OrganizationUnlocked;
use App\Models\Organization;
use App\Repositories\Contracts\OrganizationRepository;

class OrganizationEloquentRepository implements OrganizationRepository
{
    public function get($id): Organization
    {
        return Organization::with('cluster', 'projects')->findOrFail($id);
    }

    public function create(array $attributes): Organization
    {
        return Organization::create($attributes);
    }

    public function update($id, array $attributes): Organization
    {
        $organization = $this->get($id);
        if (!empty($attributes)) {
            $organization->update($attributes);
        }
        return $organization;
    }

    public function updateCurrentBillingConsumption($id, $value = null): Organization
    {
        $organization = $this->get($id);

        // Recalculate consumption on current billing period

        return $organization;
    }

    public function addBonusTransactions($id, int $qty): Organization
    {
        $organization = $this->get($id);

        // ...

        return $organization;
    }

    public function lock($id): Organization
    {
        $organization = $this->get($id);
        $organization->update(['locked_at' => now()]);
        event(new OrganizationLocked($organization));
        return $organization;
    }

    public function unlock($id): Organization
    {
        $organization = $this->get($id);
        $organization->update(['locked_at' => null]);
        event(new OrganizationUnlocked($organization));
        return $organization;
    }

    public function delete($id)
    {
        return Organization::destroy($id);
    }
}

Cache repository

namespace App\Repositories\Cache;


use App\Models\Organization;
use App\Repositories\Contracts\OrganizationRepository;
use App\Repositories\ModelCacheRepository;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Database\Eloquent\Model;

class OrganizationCacheRepository extends ModelCacheRepository implements OrganizationRepository
{
    /**
     * @var OrganizationRepository
     */
    protected $repository;

    /**
     * @var string
     */
    protected $model = Organization::class;

    /**
     * General TTL for cached items.
     */
    const CACHE_TTL = 86400; // 1 day

    /**
     * CacheOrganizationRepository constructor.
     *
     * @param Repository $cache
     * @param OrganizationRepository $repository
     */
    public function __construct(Repository $cache, OrganizationRepository $repository)
    {
        parent::__construct($cache);
        $this->repository = $repository;
    }

    /**
     * @inheritDoc
     */
    public function updateCache(Model $organization): Model
    {
        $this->cache->put($organization->getKey(), $organization);
        return $organization;
    }

    public function get($id): Organization
    {
        return $this->cache->remember($id, self::CACHE_TTL, function () use ($id) {
            return $this->repository->get($id);
        });
    }

    public function create(array $attributes): Organization
    {
        return $this->updateCache(
            $this->repository->create($attributes)
        );
    }

    // ... other methods ...
}

Container binding

In the AppServiceProvider I defined the binding of the interface with the concrete implementaiton, stacking the two repositories:

$this->app->singleton(OrganizationRepository::class, function () {
    return new OrganizationCacheRepository(
        $this->app->make(\Illuminate\Contracts\Cache\Repository::class),
        new OrganizationEloquentRepository()
    );
});

Usage

In this way I stacked the OrganizationCacheRepository up to the OrganizationEloquetRespoitory.

Now the container is able to type hint the organization’s repository when needed in my classes. Like in the controllers:

class OrganizationController extends Controller
{
    /**
     * @var OrganizationRepository
     */
    protected $repository;

    /**
     * OrganizationController constructor.
     *
     * @param OrganizationRepository $repository
     */
    public function __construct(OrganizationRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return OrganizationResource
     */
    public function index(Request $request)
    {
        return new OrganizationResource(
            $this->repository->get($request->user()->organization_id)
        );
    }

    // ... other methods ...
}

Conclusion

This architecture gave me some breathing room in terms of scalability, and made me think about what the next bottlenecks we could have in the future, as new applications were being connected to our “Code Execution Monitoring” engine almost every day.

Dealing with database is probably the most important challenge in terms of scalability. It’s not only about queries. Based on my experience it has also a big impact on the application architecture.

A cache layer in front of our SQL database has increased the amount of traffic we can handle by 5x without the need to change our infrastructure, but in many articles I have read tips like: “Add a cache layer” or “Use Redis”. Yes, it is a good suggestion, but… How? Often it’s not only about the tool, you need to understand how to change your application to embrace new technologies. Sometimes we need to recognize that code organization need to change to overcome critical performance issues.

New to Inspector?

Are you looking for a “code-driven” monitoring tool instead of having to install things at the server level?

Get a monitoring environment specifically designed for software developers avoiding any server or infrastructure configuration.

Thanks to Inspector, you will never have the need to install things at the server level or make complex configuration in your cloud infrastructure to monitor your application in real-time.

Inspector works with a lightweight software library that you can install in your application like any other dependencies. In case of Laravel you have our official Laravel package at your disposal. Developers are not always comfortable installing and configuring software at the server level, because these installations are often managed by external teams, and they are out of the software development lifecycle.

Visit our website for more details: https://inspector.dev/laravel/

Related Posts

How to prevent users from registering into your app with insecure passwords

Hi, I’m Valerio, software engineer and CTO at Inspector. About one year ago one of our accounts on an external platform has been hacked. Our credit card was attached to this account so we had to warn the bank to block it. Fortunately, there were no consequences, neither for our bank account, nor for our

Is it better to BUILD an internal monitoring environment, or BUY a prepackaged solution?

“Build vs Buy” Bake-off: Which should you choose? Every case is different, and all of these factors should be considered carefully before making a decision. There are situations where building a solution makes sense, either because you have the time, your requirements aren’t very complex, or if your applications simply don’t have enough load to

How to extend Laravel with driver-based services

Hi, I’m Valerio, software engineer and CTO at Inspector. In this article I talk about a Laravel internal feature not mentioned in the official documentation called “Driver Manager”. It can completely change the way you design and develop your application solving critical architectural bottlenecks, allowing you to build large systems built around decoupled, independent and