symfonyとHexagonalアーキテクチャ

この記事では、理論について簡単に説明し、実際には、レガシーアプリケーションを六角形のアーキテクチャに変換する方法を理解します。ナレーションはSymfonyフレームワークとPHP7.4のコンテキストで行われますが、与えられた例の構文は非常に単純なので、プログラミング言語で同じことを行う方法を簡単に理解できます(OOPをサポートしている場合)。







私のキャリアの中で私は多くのSymfonyプロジェクトに取り組んできましたが、クライアントが私たちの会社に電話する最も頻繁な問題の1つは、ソフトウェアが古いバージョンのフレームワークによって「ロック」されているか、検出して修正しているために無人になっていることです。バグは高すぎる。







通常、私はこれらのレガシープロジェクトがなぜこの状態にあるのかをよく理解しようとします。そして、よくあるパターンを見つけました。締め切りが厳しいため、プロジェクトの開始時のチームは、アプリケーションを最初からすばやく作成する必要があります。







彼らの開発プロセスは次のように始まります。







  • Composerを使用してsymfonyスケルトンプロジェクトをインストールします
  • デモコードを削除する
  • モデルの自動生成
  • コントローラの自動生成
  • これで、(ビジネスロジック)アプリケーションを開発する準備が整いました。


, - , - .







, flow .







, , Symfony ( ) , ​​ , — «domain», .







, , , :







  • ,
  • ,


, , : .









- , -, 2005 .







, ​​, , , . . ()







, : ?







, , , , . , , .









, 10 , PHP, .







PHP , composer-, -, .







, .







— (not maintainable).







.







, , , -.







, . .







« » — , - .







, , , , , .







, , , .







, , , .







/ , , .







:









class Payment
{
  public function pay(Request $request): void
  {
     $gateway = new YourBankGateway();
     $gateway->pay($request->get('amount'));
  }
}

      
      





, :







  • pay Request, - HTTP. , , , - .
  • YourBankGateway , , , , .


:









interface GatewayProvider {
    public function pay(Money $amount): void;
}

class YourBankGateway implements GatewayProvider {
    public function pay(Money $amount): void
    {
        //do stuff..
    }
}

class Payment {
    private GatewayProvider $gateway;

    public function __construct(GatewayProvider $gateway)
    {
        $this->gateway = $gateway;
    }

    public function payThroughGateway(Money $amount): void
    {
        $this->gateway->pay($amount);
   }
}

      
      





, , .







: Payment - HTTP , Money ( DTO) Request.







, .











«» () .







— , () , .







— , , .







?







  • () -


, .











, : , :







  • ,
  • ,
  • , API


, : , .







:







  • : , …


:







  • ( )


:







  • , CLI


?



. “ -” .







, .







, .









, .







( UI, API ..), (, . .). .







— .







:









interface ProductRepositoryInterface
{
   public function find(ProductId $id): ?Product;
}

      
      





— , . , .









— , , .







.







:









class MysqlProductRepository implements ProductRepositoryInterface
{
    private $repository;

    public function __construct(ProductRepository $repository)
    {
        $this->repository = $repository;
    }

    public function find(ProductId $id): ?Product
    {
        return $this->repository->find(id);
    }
}

      
      





.













, CLI HTTP-, . .







, , , .







, PHP :













: Payment Cart.







, . , .







- ( , UUID ramsey/uuid).







, .







.







, .











, , .







, . .













, .







, .







.









, :







  • , , . unit-.
  • , , , .
  • composer-, , . ., . , .
  • , .




, , , .







?



, , , .







, Infrastructure Domain.







.







, , pull- .







legacy , , :







, .







Let’s Make Our Projects Great Again



:







  • DDD (Domain-driven design)
  • CQRS ( )
  • Event sourcing
  • TDD
  • BDD


, .








All Articles