A domain module is a vertical slice of an application corresponding to a business subdomain. It contains the subdomain’s business logic and supporting API and infrastructure logic, and is owned by one team. In a modular monolith, domain modules replace top-level organization by technical layer, giving teams clearer ownership of their code. section 6.2.2 section 6.2.3
Design each module’s internal architecture, API for other modules, database schema, and build-project structure. A hexagonal structure can separate inbound adapters, domain logic, and outbound infrastructure adapters within the module. section 6.2.3 section 6.3.1
Consumer and order modules
A consumer module can expose a REST API, keep consumer entities and repository interfaces in its domain package, and use a DAO as an outbound database adapter. An order module can call a consumer service facade to reserve credit rather than invoke consumer entities and repositories directly. The facade hides implementation details, reduces design-time coupling, and is easier to mock. section 6.2.3
Figure 6.12 The ConsumerService facade encapsulates the implementation details of the Consumer Domain Module and reduces design-time coupling. Moreover, an Order Domain Module can easily mock the ConsumerService facade.
The facade provides a boundary between the order and consumer modules. section 6.2.3
Modules should generally collaborate through APIs rather than access one another’s database tables directly. A cross-module SQL join can still be worthwhile when implementation simplicity or efficiency outweighs modularity. section 6.2.3
For an operation spanning modules, choose an entry point, collaboration mechanism, and transaction strategy:
- Invoke another module directly for the simplest common collaboration, or use observation or asynchronous messaging to notify another module. section 6.2.4
- Use a single ACID transaction per operation for the simplest common transaction strategy. A saga instead sequences transactions and may require compensation when a later step fails. section 6.2.4
Creating an order
The order module handles POST /orders, collaborates with the consumer module to reserve credit, and involves the notification module to send confirmation. An order-created event can decouple order creation from notification delivery. section 6.2.4
Test modules independently with unit tests, adapter integration tests, and module tests that replace dependencies with test doubles. Keep whole-application tests limited, for example to checking that components are wired together. section 6.2.5
Do not assume each module should be one build project. If an order project depends on a consumer project, any consumer-project change can trigger order recompilation and retesting. Organize projects around stable dependencies and elements that need testing together to reduce build-time coupling. section 6.3.1
