3
2

More than 3 years have passed since last update.

Python3+CleanArchitecture

Posted at

はじめに

Python 学習のアウトプットとして、Python3 + CleanArchitectureのサンプルプログラムをコーディングしました。Injector を使って、DIP(依存関係逆転の原則)を満たすようにしています。というか今の所それだけですmm

環境&構成

.
├── main.py
├── interfaces
│   ├── controller.py  
│   └── console.py
├── usecase  
│   └── restaurant.py
└── domain
    └── message.py

処理フロー

クリーンアーキテクチャの図と、シーケンス図の色を合わせています。


  • RestaurantUseCaseは、抽象的なIORepositoryに依存する
  • RestaurantControllerのコンストラクタで、DI定義のから、RestaurantUseCaseの実装を取得する

ポイント

  • 抽象クラスのIORepositoryと、それを継承したConsoleのDIのバインド設定を定義する
  • RestaurantControllerのコンストラクタで、上記の設定でDIされたRestaurantUseCaseを保持する
class DIMoudule(Module):
    def configure(self, binder):
        binder.bind(repository.IORepository, to=console.Console)


class RestaurantController:
    def __init__(self):
        self.usecase = Injector([DIMoudule()]).get(
            restaurant.RestaurantUseCase)

    def run(self):
        self.usecase.run()
  • RestaurantUseCaseのコンストラクタで、DIされたIORepositoryの実装を保持する
    (上記のDIMouduleの設定だと、ConsoleがDIされる)
class RestaurantUseCase:
    @inject
    def __init__(self, c: repository.IORepository):
        self.console = c

    def run(self):
        self.console.start)

終わりに

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2