六边形架构——一个简单的用例

aci*_*cid 7 architecture domain-driven-design n-tier-architecture hexagonal-architecture

我已经读了很多回合六方架构和我得到最多的概念(当然,我希望我做的),我没有找到任何例子是建筑用例的明智。

假设我的应用程序域模型是为了让人陶醉。整个业务逻辑包含在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';
    }
}
Run Code Online (Sandbox Code Playgroud)

领域层还包含一个 PersonRepository

interface PersonRepository
{
    public function findPersonByName($name);
}
Run Code Online (Sandbox Code Playgroud)

实施者:

class DoctrinePersonRepository implements PersonRepository
{
    public function findPersonByName($name)
    {
        // actual retrieving
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我想通过访问让一个人醉: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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并从控制器调用它?这个提到的案例应该驻留在领域层还是应用层?在这种情况下什么是端口和适配器?如果我想有办法让这个人喝醉 - 一种来自 GET 请求,另一种来自某些php console person:drink JohnCLI 命令,该怎么办?我应该如何构建我的应用程序?

Chr*_*oll 0

是的,您所做/建议的是正确的。

\n\n
    \n
  • 用例位于应用程序层。但请注意,这是 DDD 的具体说法,与六边形架构无关。

  • \n
  • 您的适配器是

    \n\n
      \n
    1. Web 服务器和您的控制器。
    2. \n
    3. Doctrine 与 DoctrinePersonRepository 一起。
    4. \n
    5. 再次执行 php 可执行文件+您的控制器(假设您的控制器在 CLI 应用程序中重复使用)
    6. \n
  • \n
  • 你的端口:Cockburn 说“端口到底是什么和不是\xe2\x80\x99t 在很大程度上取决于品味。” 我个人认为你有 2 个端口。我将其中一个命名为持久端口,您有 1 个适配器;我将另一个端口命名为“使用”端口,您有两个适配器。

  • \n
\n\n

关于六角形建筑的单页浏览器当然是http://alistair.cockburn.us/Hexagonal+architecture

\n