How ardalis/riverbooks works
Project type and purpose
• RiverBooks is a .NET “modular monolith” sample application.
• It demonstrates how to split a single ASP.NET Core Web API into discrete, feature‐oriented modules (Books, Users, OrderProcessing, EmailSending, Reporting), all hosted in one process.
File structure
• src/RiverBooks.Web – the ASP.NET Core host (Program.cs, middleware, appsettings).
• src/RiverBooks.* (Books, Users, OrderProcessing, EmailSending, Reporting) – each module follows a consistent layering:
– Contracts (DTOs, Commands, Queries)
– Endpoints / Controllers
– UseCases or Service classes
– Domain (entities, events)
– Infrastructure/Data (EF DbContext & Migrations, repositories, caches)
– Integrations (handlers for domain/integration events)
• src/RiverBooks.SharedKernel – cross-cutting concerns (MediatR behaviors, event dispatcher).
• Tests projects for each module.
Relationships & data flows
1. HTTP Request → Web API middleware → Module Endpoint (controller) → MediatR pipeline (validators, behaviors) → Handler / Service
2. Handler → EF Repository → SQL Database ↔ Entities
3. Handler may Publish DomainEvent or IntegrationEvent → SharedKernel dispatches to IntegrationEventHandlers in same process
4. Example: CreateOrderCommand
• OrderProcessing publishes OrderCreatedIntegrationEvent
• Reporting module’s NewOrderCreatedIngestionHandler consumes it to update reports (in-memory or DB)
• OrderProcessing’s SendConfirmationEmailOrderCreatedEventHandler enqueues a SendEmailCommand into EmailSending outbox
5. EmailSendingBackgroundService reads outbox (MongoDB) → MimeKitEmailSender → SMTP server
6. Users module and OrderProcessing module share integration events for user‐address lookup; OrderProcessing caches user addresses in Redis
Architectural patterns & design principles
• Modular Monolith – feature‐based modules share a host but remain decoupled
• Onion/Clean Architecture – separation of Contracts (DTOs), Domain, Infrastructure
• CQRS via separate Commands and Queries
• Mediator pattern (MediatR) for in-process messaging
• Outbox pattern (Email outbox in MongoDB) for reliable background processing
• Use of EF Core for data access and migrations per module
• Caching level (Redis) to avoid repeated lookups