我有一个控制器,可以获取要传递给视图的数据.向此注入(通过疙瘩容器)服务,该服务使用许多域模型+业务逻辑来创建数据.
服务本身有一个注入其中的"存储库"类,它有创建数据映射器和返回域模型实例的方法.
我知道我可能没有深入了解存储库概念,因为Martin Fowler将其设置为"在映射层上构建另一层抽象"和"存储库在域和数据映射层之间进行调解,就像在-memory域对象集合." 所以我可能会错误地使用这个术语.
服务:
class InputService
{
private $repos;
public function __construct($repo) {
$this->repos = $repo;
}
public function getInitialData()
{
$product = $this->repo->getProduct();
$country = $this->repo->getCountry();
$spinalPoint = $this->repo->getPoint();
/*business logic with model instances to produce data array*/
return //array of data
}
}
Run Code Online (Sandbox Code Playgroud)
库:
class InputRepository
{
private $db;
public function __construct($db) {
$this->db = $db;
}
public function getCountry()
{
$mapper = new CountryMapper($this->db);
$country = $mapper->fetch();
return $country; //returns country object
} …
Run Code Online (Sandbox Code Playgroud) php model-view-controller datamapper decoupling repository-pattern