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
Table of contents
- Preface
- Introduction
- I. Building an Architecture to Support Domain Modeling
- 1. Domain Modeling
- 2. Repository Pattern
- 3. A Brief Interlude: On Coupling and Abstractions
-
4. Our First Use Case: Flask API and Service Layer
- Connecting Our Application to the Real World
- A First End-to-End Test
- The Straightforward Implementation
- Error Conditions That Require Database Checks
- Introducing a Service Layer, and Using FakeRepository to Unit Test It
- Why Is Everything Called a Service?
- Putting Things in Folders to See Where It All Belongs
- Wrap-Up
- 5. TDD in High Gear and Low Gear
-
6. Unit of Work Pattern
- The Unit of Work Collaborates with the Repository
- Test-Driving a UoW with Integration Tests
- Unit of Work and Its Context Manager
- Using the UoW in the Service Layer
- Explicit Tests for Commit/Rollback Behavior
- Explicit Versus Implicit Commits
- Examples: Using UoW to Group Multiple Operations into an Atomic Unit
- Tidying Up the Integration Tests
- Wrap-Up
- 7. Aggregates and Consistency Boundaries
- II. Event-Driven Architecture
- 8. Events and the Message Bus
- 9. Going to Town on the Message Bus
- 10. Commands and Command Handler
- 11. Event-Driven Architecture: Using Events to Integrate Microservices
-
12. Command-Query Responsibility Segregation (CQRS)
- Domain Models Are for Writing
- Most Users Aren’t Going to Buy Your Furniture
- Post/Redirect/Get and CQS
- Hold On to Your Lunch, Folks
- Testing CQRS Views
- “Obvious” Alternative 1: Using the Existing Repository
- Your Domain Model Is Not Optimized for Read Operations
- “Obvious” Alternative 2: Using the ORM
- SELECT N+1 and Other Performance Considerations
- Time to Completely Jump the Shark
- Changing Our Read Model Implementation Is Easy
- Wrap-Up
-
13. Dependency Injection (and Bootstrapping)
- Implicit Versus Explicit Dependencies
- Aren’t Explicit Dependencies Totally Weird and Java-y?
- Preparing Handlers: Manual DI with Closures and Partials
- An Alternative Using Classes
- A Bootstrap Script
- Message Bus Is Given Handlers at Runtime
- Using Bootstrap in Our Entrypoints
- Initializing DI in Our Tests
- Building an Adapter “Properly”: A Worked Example
- Wrap-Up
-
Epilogue
- What Now?
- How Do I Get There from Here?
- Separating Entangled Responsibilities
- Identifying Aggregates and Bounded Contexts
- An Event-Driven Approach to Go to Microservices via Strangler Pattern
- Convincing Your Stakeholders to Try Something New
- Questions Our Tech Reviewers Asked That We Couldn’t Work into Prose
- Footguns
- More Required Reading
- Wrap-Up
- A. Summary Diagram and Table
- B. A Template Project Structure
- C. Swapping Out the Infrastructure: Do Everything with CSVs
- D. Repository and Unit of Work Patterns with Django
- E. Validation
- Index
Product information
- Title: Architecture Patterns with Python
- Author(s):
- Release date: March 2020
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781492052203
You might also like
book
Python Architecture Patterns
Make the best of your test suites by using cutting-edge software architecture patterns in Python Key …
book
Using Asyncio in Python
If you’re among the Python developers put off by asyncio’s complexity, it’s time to take another …
book
Test-Driven Development with Python, 2nd Edition
By taking you through the development of a real web application from beginning to end, the …
video
Python A-Z: Learn Python by Building 15 Projects and ChatGPT
This comprehensive Python course covers all fundamental concepts and advanced Python concepts, and you learn a …