N
N
naneri2015-03-20 13:18:55
Laravel
naneri, 2015-03-20 13:18:55

So what is the advantage of the repository pattern?

I often read the statement that the main advantage of using repositories is the ability to change one repository to another. But the amount of code that needs to be changed is still the same. It's just that instead of fixing the controllers, you have to rewrite the repositories. This is especially true in the case of a partial change of storage for some models (for example, if an application that uses the MySQL database decided to move the data for personal messages, which are very numerous, to another faster storage), you have to rewrite the interaction in these models in each repository. ..
Or have I misunderstood something somewhere?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GoodBoy123, 2015-03-20
@GoodBoy123

In fact, the repository is a higher level of abstraction. When using a repository, you are programming to the interface, not to the implementation. On the example of Laravel. You have MessagesRepository:

interface MessagesRepository {

 // Выбрать все сообщение
 public function all();

 // Выбрать сообщение по id
 public function findById($id);

 //Записать  сообщение
 public function store($id, $userId);
}

In the rest of the code, you refer to this interface using the IOC container:
MessagesController.php
protected $messages;
 public function __construct(MessagesRepository $messages){
        $this->messages = $messages;
 }

public function index() {
 return view('messages.index')-with('messages', $this->messages->all());
}

After that, you use the Laravel IOC container to bind the interface to a specific implementation
$this->app->bind(
            'App\Repositories\Messages\MessagesRepository',
            'App\Repositories\Messages\MysqlMessagesRepository'
        );

And in MysqlMessagesRepository you are already doing the implementation using the interface
class MysqlMessagesRepository implements MessagesRepository {

 // Выбрать все сообщение
 public function all() {
 MysqlGetAllMessages();
}


 // Выбрать сообщение по id
 public function findById($id);

 //Записать  сообщение
 public function store($id, $userId);
}

The advantage is that the code works with the interface, not with the implementation, so in order to change Mysql to redis you need to:
create an implementation of the MessagesRepository interface
class RedisMessagesRepository implements MessagesRepository {

 // Выбрать все сообщение
 public function all() {
 RedisGetAllMessages();
}


 // Выбрать сообщение по id
 public function findById($id);

 //Записать  сообщение
 public function store($id, $userId);
}

Tell the IOC container to take a different implementation:
$this->app->bind(
            'App\Repositories\Messages\MessagesRepository',
            'App\Repositories\Messages\RedisMessagesRepository'
        );

And that is all. You don't need to change the code in MessagesController as it works with an interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question