Skip to Content
Pythonによるアーキテクチャパターン
book

Pythonによるアーキテクチャパターン

by Harry Percival, Bob Gregory
March 2025
Intermediate to advanced
304 pages
4h 33m
Japanese
O'Reilly Media, Inc.
Content preview from Pythonによるアーキテクチャパターン

第13章. 依存性の注入(とブートストラップ)

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

依存性注入(DI)はPythonの世界では疑いの目で見られている。 この本のサンプルコードでは、これまで依存性注入なしでうまくやってきた!

この章では、私たちがDIの使用を検討するきっかけとなったコードの痛点をいくつか探り、DIの方法についていくつかの選択肢を提示する。

我々はまた、bootstrap.pyと呼ばれる新しいコンポーネントを我々のアーキテクチャに追加する。それは、依存性の注入と、我々がしばしば必要とする他のいくつかの初期化を担当する。 なぜこのソートがOO言語ではコンポジション・ルートと呼ばれるのか、なぜbootstrapスクリプトが我々の目的にはちょうどいいのかを説明する。

図13-1は、ブートストラッパーなしのアプリがどのように見えるかを示している。エントリーポイントは、多くの初期化と、主要な依存関係であるUoWの受け渡しを行う。

チップ

もしまだ読んでいないのであれば、この章を読み進める前に第3章、特に関数指向とオブジェクト指向の依存関係管理の議論を読む価値がある。

apwp 1301
図13-1. Bootstrapなし:エントリーポイントは多くのことをする
チップ

この章のコードはGitHubのchapter_13_dependency_injectionブランチにある:

git clone https://github.com/cosmicpython/code.git
cd code
git checkout chapter_13_dependency_injection
# or to code along, checkout the previous chapter:
git checkout chapter_12_cqrs

図13-2は、ブートストラッパーがこれらのレスポンスを引き継いでいる様子を示している。

apwp 1302
図13-2. Bootstrapがすべてを一手に引き受ける

暗黙的依存と明示的依存

あなたの脳のタイプにもよるが、この時点で心の奥底にわずかな不安を感じているかもしれない。 それを表に出してみよう。依存関係を管理し、それをテストする2つの方法を紹介した。

データベースの依存関係については、明示的な依存関係と、それをテストでオーバーライドするための簡単なオプションという、注意深い枠組みを構築した。私たちのメインハンドラ関数は、UoWへの明示的な依存を宣言している:

我々のハンドラはUoWに明示的に依存している(src/allocation/service_layer/handlers.py)

def allocate(
        cmd: commands.Allocate, uow: unit_of_work.AbstractUnitOfWork
):

そのため、サービスレイヤーのテストで偽のUoWを入れ替えるのも簡単だ:

偽のUoWに対するサービスレイヤーのテスト: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技術リーダーシップのための14のヒント

技術リーダーシップのための14のヒント

島田 浩二
Kubernetesで実践するクラウドネイティブDevOps

Kubernetesで実践するクラウドネイティブDevOps

John Arundel, Justin Domingus, 須田 一輝, 渡邉 了介
AWS上のシステム設計

AWS上のシステム設計

Jayanth Kumar, Mandeep Singh

Publisher Resources

ISBN: 9798341624863