Answer the question
In order to leave comments, you need to log in
Where is the "Repository" pattern used?
Good afternoon. I have heard many times about the Repository pattern and how good it is, but I still don’t understand where it can be used. Could someone provide a couple of examples of its use?
PS: did I understand correctly that a repository is a class to which an object and a name are passed under which this object will be stored and with the help of this name the object can subsequently be obtained?
Answer the question
In order to leave comments, you need to log in
The repository pattern is used to isolate data storage logic. For example:
interface UserRepository {
function getUser($id);
function getUsersWhichSatisfyMyCustomBuisnessRulecs(BuisnessRules $rules);
function saveUser(User $user);
}
class InMemoryUserRepository implements UserRepository {
private $users = [];
function getUser($id) {
return isset($this->users[$id]) ?
$this->users[$id] : null;
}
function getUsersWhichSatisfyMyCustomBuisnessRulecs(BuisnessRules $rules) {
return array_filter($this->users, function (User $user) use ($rules) {
return $user->isSatisfyRule($rules->getSomeRule());
}
}
function saveUser(User $user) {
$this->users[$user->getId()] = $user;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question