reinink
1347
47
Laravel

Are repositories really the way?

Posted 4 years ago by reinink

So I've been thinking about repositories recently, and questioning their usefulness when working with Eloquent (or really any ORM). Some thoughts:

1. Repositories allow you to swap out your data source (rely on interfaces)

While this is maybe a strong argument for some, in my experience it isn't. I don't ever plan on changing my ORM, at least not without a major rewrite. And Eloquent (along with any decent ORM) already supports multiple database types, so I still have the freedom to swap from MySQL to PostgreSQL to SQlite.

2. Repositories help keep your controllers slimmer

This is true, but there are other ways to accomplish this. For example, you could simply break a more complicated task into a command/job or query object. In fact, I find this is often clearer, especially when there is business logic that goes with it.

3. Repositories make for easier testing

So, the thinking here is that I use an ArrayUsersRepository instead of my EloquentUsersRepository when testing so I don't have to hit my actual database. But I literally never do that. I simply mock out my UsersRepositoryInterace. But the thing is, this can be done with Eloquent objects as well. Ideally you would inject your Eloquent objects into your class, but technically it's even possible to mock the Facades.

So, and I missing something? It seems many are actually using the DAO pattern and calling it the repository pattern. What do you think?

Please sign in or create an account to participate in this conversation.