Architecture Patterns with Python

Book description

As Python continues to grow in popularity, projects are becoming larger and more complex. Many Python developers are taking an interest in high-level software design patterns such as hexagonal/clean architecture, event-driven architecture, and the strategic patterns prescribed by domain-driven design (DDD). But translating those patterns into Python isn’t always straightforward.

With this hands-on guide, Harry Percival and Bob Gregory from MADE.com introduce proven architectural design patterns to help Python developers manage application complexity—and get the most value out of their test suites.

Each pattern is illustrated with concrete examples in beautiful, idiomatic Python, avoiding some of the verbosity of Java and C# syntax. Patterns include:

  • Dependency inversion and its links to ports and adapters (hexagonal/clean architecture)
  • Domain-driven design’s distinction between Entities, Value Objects, and Aggregates
  • Repository and Unit of Work patterns for persistent storage
  • Events, commands, and the message bus
  • Command-query responsibility segregation (CQRS)
  • Event-driven architecture and reactive microservices

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Managing Complexity, Solving Business Problems
    2. Why Python?
    3. TDD, DDD, and Event-Driven Architecture
    4. Who Should Read This Book
    5. A Brief Overview of What You’ll Learn
      1. Part I, Building an Architecture to Support Domain Modeling
      2. Part II, Event-Driven Architecture
      3. Addtional Content
    6. Example Code and Coding Along
    7. License
    8. Conventions Used in This Book
    9. O’Reilly Online Learning
    10. How to Contact O’Reilly
    11. Acknowledgments
  2. Introduction
    1. Why Do Our Designs Go Wrong?
    2. Encapsulation and Abstractions
    3. Layering
    4. The Dependency Inversion Principle
    5. A Place for All Our Business Logic: The Domain Model
  3. I. Building an Architecture to Support Domain Modeling
  4. 1. Domain Modeling
    1. What Is a Domain Model?
    2. Exploring the Domain Language
    3. Unit Testing Domain Models
      1. Dataclasses Are Great for Value Objects
      2. Value Objects and Entities
    4. Not Everything Has to Be an Object: A Domain Service Function
      1. Python’s Magic Methods Let Us Use Our Models with Idiomatic Python
      2. Exceptions Can Express Domain Concepts Too
  5. 2. Repository Pattern
    1. Persisting Our Domain Model
    2. Some Pseudocode: What Are We Going to Need?
    3. Applying the DIP to Data Access
    4. Reminder: Our Model
      1. The “Normal” ORM Way: Model Depends on ORM
      2. Inverting the Dependency: ORM Depends on Model
    5. Introducing the Repository Pattern
      1. The Repository in the Abstract
      2. What Is the Trade-Off?
    6. Building a Fake Repository for Tests Is Now Trivial!
    7. What Is a Port and What Is an Adapter, in Python?
    8. Wrap-Up
  6. 3. A Brief Interlude: On Coupling and Abstractions
    1. Abstracting State Aids Testability
    2. Choosing the Right Abstraction(s)
    3. Implementing Our Chosen Abstractions
      1. Testing Edge to Edge with Fakes and Dependency Injection
      2. Why Not Just Patch It Out?
    4. Wrap-Up
  7. 4. Our First Use Case: Flask API and Service Layer
    1. Connecting Our Application to the Real World
    2. A First End-to-End Test
    3. The Straightforward Implementation
    4. Error Conditions That Require Database Checks
    5. Introducing a Service Layer, and Using FakeRepository to Unit Test It
      1. A Typical Service Function
    6. Why Is Everything Called a Service?
    7. Putting Things in Folders to See Where It All Belongs
    8. Wrap-Up
      1. The DIP in Action
  8. 5. TDD in High Gear and Low Gear
    1. How Is Our Test Pyramid Looking?
    2. Should Domain Layer Tests Move to the Service Layer?
    3. On Deciding What Kind of Tests to Write
    4. High and Low Gear
    5. Fully Decoupling the Service-Layer Tests from the Domain
      1. Mitigation: Keep All Domain Dependencies in Fixture Functions
      2. Adding a Missing Service
    6. Carrying the Improvement Through to the E2E Tests
    7. Wrap-Up
  9. 6. Unit of Work Pattern
    1. The Unit of Work Collaborates with the Repository
    2. Test-Driving a UoW with Integration Tests
    3. Unit of Work and Its Context Manager
      1. The Real Unit of Work Uses SQLAlchemy Sessions
      2. Fake Unit of Work for Testing
    4. Using the UoW in the Service Layer
    5. Explicit Tests for Commit/Rollback Behavior
    6. Explicit Versus Implicit Commits
    7. Examples: Using UoW to Group Multiple Operations into an Atomic Unit
      1. Example 1: Reallocate
      2. Example 2: Change Batch Quantity
    8. Tidying Up the Integration Tests
    9. Wrap-Up
  10. 7. Aggregates and Consistency Boundaries
    1. Why Not Just Run Everything in a Spreadsheet?
    2. Invariants, Constraints, and Consistency
      1. Invariants, Concurrency, and Locks
    3. What Is an Aggregate?
    4. Choosing an Aggregate
    5. One Aggregate = One Repository
    6. What About Performance?
    7. Optimistic Concurrency with Version Numbers
      1. Implementation Options for Version Numbers
    8. Testing for Our Data Integrity Rules
      1. Enforcing Concurrency Rules by Using Database Transaction Isolation Levels
      2. Pessimistic Concurrency Control Example: SELECT FOR UPDATE
    9. Wrap-Up
    10. Part I Recap
  11. II. Event-Driven Architecture
  12. 8. Events and the Message Bus
    1. Avoiding Making a Mess
      1. First, Let’s Avoid Making a Mess of Our Web Controllers
      2. And Let’s Not Make a Mess of Our Model Either
      3. Or the Service Layer!
    2. Single Responsibility Principle
    3. All Aboard the Message Bus!
      1. The Model Records Events
      2. Events Are Simple Dataclasses
      3. The Model Raises Events
      4. The Message Bus Maps Events to Handlers
    4. Option 1: The Service Layer Takes Events from the Model and Puts Them on the Message Bus
    5. Option 2: The Service Layer Raises Its Own Events
    6. Option 3: The UoW Publishes Events to the Message Bus
    7. Wrap-Up
  13. 9. Going to Town on the Message Bus
    1. A New Requirement Leads Us to a New Architecture
      1. Imagining an Architecture Change: Everything Will Be an Event Handler
    2. Refactoring Service Functions to Message Handlers
      1. The Message Bus Now Collects Events from the UoW
      2. Our Tests Are All Written in Terms of Events Too
      3. A Temporary Ugly Hack: The Message Bus Has to Return Results
      4. Modifying Our API to Work with Events
    3. Implementing Our New Requirement
      1. Our New Event
    4. Test-Driving a New Handler
      1. Implementation
      2. A New Method on the Domain Model
    5. Optionally: Unit Testing Event Handlers in Isolation with a Fake Message Bus
    6. Wrap-Up
      1. What Have We Achieved?
      2. Why Have We Achieved?
  14. 10. Commands and Command Handler
    1. Commands and Events
    2. Differences in Exception Handling
    3. Discussion: Events, Commands, and Error Handling
    4. Recovering from Errors Synchronously
    5. Wrap-Up
  15. 11. Event-Driven Architecture: Using Events to Integrate Microservices
    1. Distributed Ball of Mud, and Thinking in Nouns
    2. Error Handling in Distributed Systems
    3. The Alternative: Temporal Decoupling Using Asynchronous Messaging
    4. Using a Redis Pub/Sub Channel for Integration
    5. Test-Driving It All Using an End-to-End Test
      1. Redis Is Another Thin Adapter Around Our Message Bus
      2. Our New Outgoing Event
    6. Internal Versus External Events
    7. Wrap-Up
  16. 12. Command-Query Responsibility Segregation (CQRS)
    1. Domain Models Are for Writing
    2. Most Users Aren’t Going to Buy Your Furniture
    3. Post/Redirect/Get and CQS
    4. Hold On to Your Lunch, Folks
    5. Testing CQRS Views
    6. “Obvious” Alternative 1: Using the Existing Repository
    7. Your Domain Model Is Not Optimized for Read Operations
    8. “Obvious” Alternative 2: Using the ORM
    9. SELECT N+1 and Other Performance Considerations
    10. Time to Completely Jump the Shark
      1. Updating a Read Model Table Using an Event Handler
    11. Changing Our Read Model Implementation Is Easy
    12. Wrap-Up
  17. 13. Dependency Injection (and Bootstrapping)
    1. Implicit Versus Explicit Dependencies
    2. Aren’t Explicit Dependencies Totally Weird and Java-y?
    3. Preparing Handlers: Manual DI with Closures and Partials
    4. An Alternative Using Classes
    5. A Bootstrap Script
    6. Message Bus Is Given Handlers at Runtime
    7. Using Bootstrap in Our Entrypoints
    8. Initializing DI in Our Tests
    9. Building an Adapter “Properly”: A Worked Example
      1. Define the Abstract and Concrete Implementations
      2. Make a Fake Version for Your Tests
      3. Figure Out How to Integration Test the Real Thing
    10. Wrap-Up
  18. Epilogue
    1. What Now?
    2. How Do I Get There from Here?
    3. Separating Entangled Responsibilities
    4. Identifying Aggregates and Bounded Contexts
    5. An Event-Driven Approach to Go to Microservices via Strangler Pattern
    6. Convincing Your Stakeholders to Try Something New
    7. Questions Our Tech Reviewers Asked That We Couldn’t Work into Prose
    8. Footguns
    9. More Required Reading
    10. Wrap-Up
  19. A. Summary Diagram and Table
  20. B. A Template Project Structure
    1. Env Vars, 12-Factor, and Config, Inside and Outside Containers
    2. Config.py
    3. Docker-Compose and Containers Config
    4. Installing Your Source as a Package
    5. Dockerfile
    6. Tests
    7. Wrap-Up
  21. C. Swapping Out the Infrastructure: Do Everything with CSVs
    1. Implementing a Repository and Unit of Work for CSVs
  22. D. Repository and Unit of Work Patterns with Django
    1. Repository Pattern with Django
      1. Custom Methods on Django ORM Classes to Translate to/from Our Domain Model
    2. Unit of Work Pattern with Django
    3. API: Django Views Are Adapters
    4. Why Was This All So Hard?
    5. What to Do If You Already Have Django
    6. Steps Along the Way
  23. E. Validation
    1. What Is Validation, Anyway?
    2. Validating Syntax
    3. Postel’s Law and the Tolerant Reader Pattern
    4. Validating at the Edge
    5. Validating Semantics
    6. Validating Pragmatics
  24. Index

Product information

  • Title: Architecture Patterns with Python
  • Author(s): Harry Percival, Bob Gregory
  • Release date: March 2020
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492052203