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によるアーキテクチャパターン

第6章. ユニット・オブ・ワーク・パターン

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

この章では、リポジトリパターンとサービスレイヤーパターンを結びつけるパズルの最後のピース、Unit of Workパターンを紹介する。

Repositoryパターンが永続ストレージの概念を抽象化したものだとすれば、Unit of Work(UoW)パターンは原子性演算子の概念を抽象化したものである。 これによって、最終的にサービス・レイヤーをデータ・レイヤーから完全に切り離すことができる。

図6-1は、現在、多くの通信がインフラのレイヤーをまたがって発生していることを示している。APIはデータベースレイヤーと直接会話してセッションを開始し、リポジトリレイヤーと会話してSQLAlchemyRepository を初期化し、サービスレイヤーと会話して割り当てを依頼する。

チップ

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

git clone https://github.com/cosmicpython/code.git
cd code
git checkout chapter_06_uow
# or to code along, checkout Chapter 4:
git checkout chapter_04_service_layer
apwp 0601
図6-1. UoWなし:APIは3つのレイヤーと直接会話する。

図6-2はターゲット状態を示している。FlaskのAPIが行うことは2つだけだ:ユニット・オブ・ワークを初期化することと、サービスを呼び出すことだ。サービスはUoWと連携するが(UoWはサービスレイヤーの一部だと考えたい)、サービス関数自体もFlaskもデータベースと直接話す必要はない。

そして、Pythonの構文の素敵な部分、コンテキスト・マネージャー( )を使って全てを行う。

apwp 0602
図6-2. UoWで:UoWがデータベースの状態を管理するようになった

作業単位はリポジトリと連携する

それでは、ユニット・オブ・ワーク(UoW、私たちは「ユーウー」と発音する)を実際に見てみよう。 レイヤーが完成したら、こんな感じになる:

作業単位の動作プレビュー(src/allocation/service_layer/services.py)

def allocate(
        orderid: str, sku: str, qty: int,
        uow: unit_of_work.AbstractUnitOfWork
) -> str:
    line = OrderLine(orderid, sku, qty)
    with uow:  1
        batches 
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