Я начинаю новый проект, и мне нужна помощь в определении шаблона следующих PHP классов (хотя я не думаю, что есть что-то PHP специфицирующее c помимо синтаксиса). Пример взят здесь: https://github.com/PHP-DI/demo. Я не беспокоюсь о качестве КОДА, меня интересует реализация шаблона и, возможно, альтернативы этому конкретному шаблону.
<?php
// Article.php
namespace SuperBlog\Model;
class Article
{
private $id;
private $title;
private $content;
public function __construct($id, $title, $content)
{
$this->id = $id;
$this->title = $title;
$this->content = $content;
}
public function getId()
{
return $this->id;
}
public function getTitle()
{
return $this->title;
}
public function getContent()
{
return $this->content;
}
}
<?php
// ArticleRepository.php
namespace SuperBlog\Model;
/**
* This is an interface so that the model is coupled to a specific backend.
*
* This is also so that we can demonstrate how to bind an interface
* to an implementation with PHP-DI.
*/
interface ArticleRepository
{
/**
* @return Article[]
*/
public function getArticles();
/**
* @param string $id
* @return Article
*/
public function getArticle($id);
}
<?php
// InMemoryArticleRepository.php
namespace SuperBlog\Persistence;
use SuperBlog\Model\Article;
use SuperBlog\Model\ArticleRepository;
class InMemoryArticleRepository implements ArticleRepository
{
private $articles;
public function __construct()
{
$this->articles = [
1 => new Article(1, 'Hello world!', 'This article is here to welcome you.'),
2 => new Article(2, 'There is something new!', 'Here is a another article.'),
];
}
public function getArticles()
{
return $this->articles;
}
public function getArticle($id)
{
return $this->articles[$id];
}
}
С вершины моего голова, я думал следующее:
Article.php
это модель домена, которая будет жить на уровне домена. Конкретная реализация (в репозитории памяти) вернет эту модель в ответ на запрос репозитория.
ArticleRepository.php
: это интерфейс, который будет жить на уровне домена, чтобы учитывать внедрение зависимостей и устранять зависимость от данных. реализация модели.
InMemoryArticleRepository.php
: это модель данных и конкретная реализация интерфейса репозитория, которая будет внедрена в уровень домена через DI.
Правильно ли мое мнение выше (маловероятно ) или требуется коррекция (более вероятно)?