Answer the question
In order to leave comments, you need to log in
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
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);
}
protected $messages;
public function __construct(MessagesRepository $messages){
$this->messages = $messages;
}
public function index() {
return view('messages.index')-with('messages', $this->messages->all());
}
$this->app->bind(
'App\Repositories\Messages\MessagesRepository',
'App\Repositories\Messages\MysqlMessagesRepository'
);
class MysqlMessagesRepository implements MessagesRepository {
// Выбрать все сообщение
public function all() {
MysqlGetAllMessages();
}
// Выбрать сообщение по id
public function findById($id);
//Записать сообщение
public function store($id, $userId);
}
class RedisMessagesRepository implements MessagesRepository {
// Выбрать все сообщение
public function all() {
RedisGetAllMessages();
}
// Выбрать сообщение по id
public function findById($id);
//Записать сообщение
public function store($id, $userId);
}
$this->app->bind(
'App\Repositories\Messages\MessagesRepository',
'App\Repositories\Messages\RedisMessagesRepository'
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question