DEV Community

Discussion on: Repository Pattern in Laravel, it's worth?

Collapse
 
angry_ferret profile image
Pavel • Edited

Seems like you've missed the true purpose of this pattern. But first, Doctrine is not based on the Repository pattern. The pattern it's based on is called Data Mapper. This is actually written in Doctrine's documentation. Repositories and entity managers are just parts of this bigger concept.
Now to the Repository pattern. The main purpose of this pattern is to have an extra-layer that would abstract away a particular Active Record ORM from the rest of the application. By doing this we:

  • have the ability to easily swap the ORM (or even the entire storage technology)
  • restrict most of our application from being able to directly manipulate the data in DB . This is especially important in applications with layered architecture, where you don't want any of the layers except Persistence to have direct access to the data in the storage. We can pass Doctrine entities across the entire application if we need to. It's not possible to do anything with data in the storage through entity's API alone because these are just plain old PHP objects. But it's possible in case we have AR-model instead (because they have methods like ::save(), ::delete() etc.).

By returning an Eloquent model instance from UserRepository::find() method (instead of, say, DTO) you create the so called leaky abstraction and defeat the general idea behind a repository. That's why, I assume, you don't understand the concept in general.

Collapse
 
mondayrris profile image
mondayrris • Edited

Totally agree. Apart form DTO, this article seems not to state the existence of ValueObjects, RepositoryInterfaces and Facades, which is useful in repository pattern