As far as I know, The main goal of using repository pattern is changing the DBMS easily, right?
for example, when someday we want to use another DBMS instead of MySQL.
Isn't that possible with changing the database driver in Laravel config files? Eloquent does handle the rest right?
I love eloquent API, and don't want to build a lot of classes and interfaces used by Repository Pattern.
I wanna know is the Repository Pattern worth to give up eloquent API and write and maintain so much more code?
As far as I know, The main goal of using repository pattern is changing the DBMS easily, right?
I would not agree on this one. You can combine it with ActiveRecord pattern and for example use Repository only for getting the data and active record for persisting the data. It all comes down to personal preference and what makes sense for your application. This way you would remove additional clutter from Models and avoid having to remember which scopes you have to use in order to get the desired data which in repositories is hidden behind hopefully well named method.
Isn't that possible with changing the database driver in Laravel config files? Eloquent does handle the rest right?
Yes changing the database driver does handle everything regarding switching to other database. And yes eloquent is able to talk to many databases.
I love eloquent API, and don't want to build a lot of classes and interfaces used by Repository Pattern.
You do not have to create a lot of classes and interfaces. It all comes down to your own implementation.
I wanna know is the Repository Pattern worth to give up eloquent API?
You are not giving up Eloquent API cause you need logic which repository will use to talk to databases. It is far easier to use Eloquent in repository methods than to use something else cause there is no need for that.
I know it depends on the implementation, but still, it has restrictions and adds some level of complexity to the code.
using Repository Pattern with Eloquent (an Active Record Pattern) doesn't seem right. If I'm gonna use Eloquent in repositories to talk to the database, what should I use this additional layer? why bother?
See? there are a lot of additional classes needed.
and I should write code for every Eloquent Scenario.
using Repository Pattern with Eloquent (an Active Record Pattern) doesn't seem right. If I'm gonna use Eloquent in repositories to talk to the database, what should I use this additional layer? why bother?
?
What additional benefits the Repository Pattern has IN LARAVEL that makes it worth to use?
Repository Pattern will make you centralize your data access layer..
so you will be seperating your business logic from it.. ( you won't care how data is being retrieved)
For example , if you working on a project where you use a back end service like CRM to manipulate your data..
then you suddently decided to switch for MYSQL or MongoDB.. you will be changing just one single class instead of the N classes affected...
To me it is far easier to test repository cause they tend to have less methods than if you are going to test every scope separately. And sometimes separate scopes do not make sense, but when combined with others they do. So what you will have to do in that situation is to test separate and combined scopes which then multiples the number of tests you have to write. Also naming scopes is harder than naming one single method in repository.
My English does not allow me to write this any better so I hope you get my point.
For example , if you working on a project where you use a back end service like CRM to manipulate your data.. then you suddenly decided to switch for MYSQL or MongoDB.. you will be changing just one single class instead of the N classes affected...
and In Laravel using Eloquent you can change the database driver in config files and that's it, done. Eloquent can talk to various DBMSs.
So If I have this goal in mind (changing the DBMS easily), using Repository Pattern in Laravel is not reasonable. because Eloquent has that ability.
So what you will have to do in that situation is to test separate and combined scopes which then multiples the number of tests you have to write.
in case of writing tests, when using Eloquent in Repository Pattern, it's like doing the same with a layer in between. it's like writing tests for Eloquent Model Methods indirectly.
I don't want to just fight over it. I just want to really understand why using Repository Pattern is better in Laravel (if it is better).
No one can claim that it is better since there is no proof that it is. All the value is in the hands of the developers. If they like it then it makes sense. If not then it does not make sense.