Architecture
The Ordering service is built using Domain-Driven Design (DDD) and Clean Architecture principles, organized into four distinct layers:Core Responsibilities
- Order Management: Create, update, delete, and query orders
- Domain Logic: Enforce business rules through rich domain models
- Event Processing: Consume BasketCheckout events and publish order events
- Data Persistence: Store orders using Entity Framework Core with SQL Server
- CQRS Implementation: Separate read and write operations with MediatR
Technology Stack
Frameworks & Libraries
- .NET 8: Latest LTS version for high performance
- Entity Framework Core: ORM for database access
- MediatR: CQRS pattern implementation
- Carter: Minimal API endpoint organization
- MassTransit: Message broker integration
- FluentValidation: Input validation
- Mapster: Object-to-object mapping
Patterns & Practices
- Domain-Driven Design (DDD): Rich domain models with aggregates
- Clean Architecture: Dependency inversion and separation of concerns
- CQRS: Command Query Responsibility Segregation
- Repository Pattern: Data access abstraction
- Domain Events: Decoupled business logic
- Value Objects: Immutable domain primitives
- Aggregate Root: Order as the consistency boundary
Project Structure
Domain Layer
Application Layer
Infrastructure Layer
API Layer
Service Dependencies
Integration with Other Services
Message Consumption
Consumes:BasketCheckoutEvent from Basket service
- Triggers order creation when a customer checks out their basket
- Maps basket data to order aggregate
- Initiates order fulfillment process
Message Publishing
Publishes:OrderDto (Order Created Integration Event)
- Published when a new order is created
- Feature flag controlled:
OrderFullfilment - Can be consumed by fulfillment, notification, or other services
Database Schema
The service uses SQL Server with Entity Framework Core, storing:- Orders: Order aggregate roots with shipping, billing, and payment info
- OrderItems: Line items within orders
- Customers: Customer information
- Products: Product reference data
AuditableEntityInterceptor.
Key Features
Domain Event Handling
Domain events are dispatched automatically duringSaveChanges through the DispatchDomainEventsInterceptor, ensuring:
- Events are published only when changes are persisted
- Transaction consistency between state changes and events
- Decoupled domain logic through event handlers
Validation
Multi-layer validation ensures data integrity:- Domain Layer: Business rule validation in aggregates and value objects
- Application Layer: FluentValidation validators on commands
- Value Objects: Guard clauses prevent invalid state
CQRS Separation
- Commands: Modify state, return simple results (CreateOrderCommand, UpdateOrderCommand)
- Queries: Read-only operations with optimized projections (GetOrdersQuery, GetOrdersByCustomerQuery)
- MediatR: Mediates between API endpoints and handlers
Next Steps
- Domain Model - Explore entities, value objects, and aggregates
- Application Layer - Learn about CQRS commands, queries, and event handlers
- API Reference - View all REST endpoints and contracts
