-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document memory-efficient Entity-Repository pattern #240
Comments
Original issue text: The repository pattern is a design pattern that describes an object that represents a mutable list of data, and is perfect for answering the questions raised by #80. Problem: Solution: Use of class CustomerRepository {
protected $db;
public function __construct(QueryCollection $db) {
$this->db = $db;
}
public function getById(int $id):Customer {
$row = $this->db->fetch("getById", $id);
return new Customer(
$row->firstName,
$row->lastName,
// ... etc.
);
}
} The idea is simple: let the developer have complete control over how the Entity objects are created. For example, it may not be that all Entity object constructors take a Row parameter... so this allows arbitrary classes to be used as Entities. Strict rule: All database mutations should be done within their own Repository classes. Entities can have functionality, but only functionality that is calculating something based on its own internal data structure. |
<?php
abstract class Entity {}
class User extends Entity {
public function getUsername():string { return "bob"; }
}
/** @template T */
class Repository {
/** @return T */
public function getById(string|int $id):Entity {
}
}
/** @extends Repository<User> */
class UserRepository extends Repository {
}
$userRepo = new UserRepository();
$user = $userRepo->getById(123);
// This is now type-hinted correctly!
$user->getUsername(); |
The Entity-Repository pattern is perfect for complementing usage of this repo, but something not obvious from the Entity-Repository Pattern is how it can be utilised in a memory-efficient way.
Basically, any function that returns a collection of Entities should do it via a callback to the original ResultSet.
The text was updated successfully, but these errors were encountered: