Overview
OrderServices is a record that bundles all external service dependencies required by the Order aggregate. It provides a clean dependency injection point and ensures orders have access to pricing, inventory, payment, and fulfillment capabilities.
Record Definition
Components
pricing
Service responsible for calculating order line prices.Pricing service implementationMethods:
calculatePrice(PricingContext)- ReturnsOrderLinePricingbased on product, quantity, customer, and context
inventory
Service managing product availability and allocation.Inventory service implementationMethods:
allocate(AllocationRequest)- Reserves inventory for order lines- Returns
AllocationResultwith status (ALLOCATED, INSUFFICIENT_QUANTITY, etc.)
payment
Service handling payment authorization and capture.Payment service implementationMethods:
authorizeAndCapture(PaymentRequest)- Processes payment for order total- Returns
PaymentResultwith status (CAPTURED, DECLINED, ERROR, etc.)
fulfillment
Service orchestrating order fulfillment operations.Fulfillment service implementationMethods:
startFulfillment(OrderId)- Initiates fulfillment processcancelFulfillment(OrderId)- Cancels ongoing fulfillment
Creating OrderServices
SinceOrderServices is a record, it’s created using the canonical constructor:
Usage Examples
Spring Configuration
Creating Orders with Services
Test Doubles
Mocking for Tests
Service Orchestration Flow
TheOrder aggregate orchestrates services in a specific sequence during confirmation:
Design Considerations
Service Boundaries
Each service represents a distinct bounded context:- Pricing: Product catalog, pricing rules, discounts
- Inventory: Stock levels, warehouses, reservations
- Payment: Payment processing, billing, transactions
- Fulfillment: Shipping, provisioning, delivery
Dependency Injection
The record pattern provides:- Immutability: Services can’t be swapped after order creation
- Type Safety: Compiler enforces all services are provided
- Clarity: Explicit dependencies visible in constructor
Failure Handling
Services should return result objects rather than throwing exceptions for business failures:Transactional Boundaries
The order aggregate should typically be saved within a transaction that includes service calls:Related Types
- PricingService: Calculates prices for order lines
- InventoryService: Manages product availability and allocation
- PaymentService: Processes payments and refunds
- FulfillmentService: Orchestrates order fulfillment (see Fulfillment Service)
- Order: Main aggregate using these services (see Order)
Service Interface References
For detailed documentation on each service interface:- FulfillmentService - Order fulfillment operations
- PricingService - Located in
/workspace/source/ordering/src/main/java/com/softwarearchetypes/ordering/PricingService.java - InventoryService - Located in
/workspace/source/ordering/src/main/java/com/softwarearchetypes/ordering/InventoryService.java - PaymentService - Located in
/workspace/source/ordering/src/main/java/com/softwarearchetypes/ordering/PaymentService.java
