What Is the Repository Pattern?
The Repository pattern is a way of abstracting your business logic away from your data source. It is an extra layer on top of your data-retrieval code and can be used in a variety of ways.
Why Use This Pattern?
The goal of a repository is to increase code maintainability and to shape your code around your application's use cases.
Many developers think of it as a tool only needed for larger applications, but I use it for most applications. The benefits outweigh the cost of writing extra code.
Let's go over some of its benefits:
Dependency Inversion
This is an expression of the SOLID principles. The repository pattern lets us create many suitable implementations of an interface whose purpose is to handle our data.
The most frequently cited use case is “switching your data source”. This describes being able to move from one data store to another, say from MySQL to a NoSQL database, without affecting your application code.
This is achieved by creating an interface for retrieving your data. You can then create one or more implementations of that interface.
For example, for the logic in our article repository, we will create an implementation that uses Eloquent. If we ever need to switch our data source to MongoDB, we can create a MongoDB implementation and swap it in. Since our application code expects an interface rather than a concrete class, it won't notice the difference when we switch from one implementation to another.
If you use your data store in many places in your application (which you most likely do), this has a very powerful effect. Chances are you interact with your data on almost every request to your application, whether in a controller, a command, a queued job or your form-handling code.
If you can be sure that every part of your application will always have the methods it needs, moving forward will be much easier.
But in reality, how often do you change your data source? You rarely replace your core SQL-based data source. Still, there are other reasons to use the repository pattern.
Planning
I mentioned earlier that my approach to writing applications centers on business logic. Creating an interface is very helpful for planning your code around your business requirements (use cases).
When you define the methods every implementation will use, you are planning the use cases of your domain. Will users be able to create articles? Or will they only be able to read them? How will administrators interact with the system differently from regular users?
Defining interfaces gives you a clearer picture of how your application will be used from a domain perspective rather than a data perspective.
Business Logic Orientation
Building on the idea of planning around the business domain means actually expressing your business domain in code. Remember that each Eloquent model represents a single database table. Your business logic does not.
For example, an article in our sample application is more than a row in our articles table. It also includes an author from the users table, a status from the statuses table and a set of tags represented in the tags and articles_tags tables. An article is a composite business entity: one that contains other entities beyond its own attributes such as its title, content and publication date.
The repository pattern lets us express an article as more than a single row in a table. We can combine Eloquent models, their relationships and the query builder in whatever way it takes to turn raw data into true representations of our business entities.
Data Layer Logic
The data repository is a very convenient place for other logic related to retrieving data. Rather than adding extra logic to our Eloquent models, we can use our repository to house it.
For example, you may need to cache your data. Our data repository is also a great place to add your caching layer.
Remote Data
It is important to keep in mind that your data can come from many sources, not necessarily your own database.
Many modern web applications are mash-ups. They consume multiple APIs and present useful data to the end user. A data repository can also be a way to house the logic of fetching API data and combining it into an entity your application can easily use or process.
It is also useful in a Service Oriented Architecture (SOA), where we may need to make API calls to services within our own infrastructure but have no direct access to a database.