我已经阅读了很多关于六边形架构的文章,并且我确实了解了大部分概念(嗯,我希望我这样做了),但我没有找到任何关于该架构用例的示例。
假设我的应用领域模型是让人醉的。整个业务逻辑包含在Person
位于领域层的类中。
class Person
{
private $name;
private $age;
function __construct($name, $age)
{
$this->age = $age;
$this->name = $name;
}
public function drink()
{
if ($this->age < 18) {
echo $this->name . ' cant drink';
}
echo $this->name . ' drinks tequila';
}
}
领域层还包含一个PersonRepository
interface PersonRepository
{
public function findPersonByName($name);
}
实施者:
class DoctrinePersonRepository implements PersonRepository
{
public function findPersonByName($name)
{
// actual retrieving
}
}
假设我想通过访问使一个人喝醉:GET /person/johnDoe/drink
。我应该创建一个像这样的用例:
class MakePersonDrinkCase
{
/**
* @var PersonRepository
*/
private $personRepository;
function __construct(PersonRepository $personRepository)
{
$this->personRepository = $personRepository;
}
function makePersonDrunk($name)
{
$person = $this->personRepository->findPersonByName($name);
if ($name) {
throw new \Exception('Person not found');
}
$person->drink();
}
}
并从控制器调用它?这个提到的案例应该驻留在领域层还是应用层?在这种情况下,什么是端口和适配器?如果我想有办法让这个人喝醉——一种来自 GET 请求,另一种来自某些php console person:drink John
CLI 命令?我应该如何构建我的应用程序?