How kgrzybek/modular-monolith-with-ddd works
Purpose and Runtime Entry Points
This is a .NET 8 modular monolith implementing a Meetup-like meeting-groups domain with DDD, CQRS, asynchronous module integration, and event sourcing for Payments. The executable HTTP host is the ASP.NET Core REST API at src/API/CompanyName.MyMeetings.API/Program.cs and src/API/CompanyName.MyMeetings.API/Startup.cs. Docker starts the API, SQL Server, and database migrator through docker-compose.yml.
API Host and Boundary
The API is deliberately thin: controllers authenticate/authorize, map HTTP requests to module commands/queries, and return responses. It initializes module composition roots rather than containing business logic. Cross-cutting request concerns include correlation IDs, permission authorization, Swagger, and problem-details validation responses.
• src/API/CompanyName.MyMeetings.API/Startup.cs
• src/API/CompanyName.MyMeetings.API/Configuration/Authorization/HasPermissionAuthorizationHandler.cs
• src/API/CompanyName.MyMeetings.API/Modules/Meetings/Meetings/MeetingsController.cs
Bounded-Context Modules
Six isolated modules own their domain, application contracts, infrastructure, and database schema: Meetings (groups, meetings, attendance/comments), Administration (proposal approval), Payments (subscriptions and fees), Registrations (registration confirmation), and UserAccess (identity/authentication); API delegates through their public module interfaces. Each module has an independent Autofac composition root and exposes only contracts/integration-event assemblies.
• src/Modules/Meetings/Infrastructure/MeetingsModule.cs
• src/Modules/Administration/Infrastructure/AdministrationModule.cs
• src/Modules/Payments/Infrastructure/PaymentsModule.cs
CQRS and Rich Write Models
Commands traverse MediatR handlers, validation/logging/unit-of-work decorators, repositories, and DDD aggregates that enforce business rules and emit domain events. Reads bypass aggregates, using Dapper/SQL against module-owned views. This is the principal write/read boundary.
• src/Modules/Meetings/Application/Meetings/CreateMeeting/CreateMeetingCommandHandler.cs
• src/Modules/Meetings/Domain/Meetings/Meeting.cs
• src/Modules/Meetings/Application/MeetingGroups/GetAllMeetingGroups/GetAllMeetingGroupsQueryHandler.cs
Asynchronous Integration and Background Processing
Modules do not make direct calls to each other. They publish typed integration events through the in-memory event bus; each module persists outgoing messages in an outbox and consumes incoming messages through an inbox for at-least-once delivery/processing. Quartz jobs drive inbox, outbox, and internal-command processing, creating eventual-consistency boundaries.
• src/BuildingBlocks/Infrastructure/EventBus/InMemoryEventBus.cs
• src/Modules/Meetings/Infrastructure/Configuration/Processing/Outbox/ProcessOutboxJob.cs
• src/Modules/Registrations/IntegrationEvents/NewUserRegisteredIntegrationEvent.cs
Identity, Authorization, and Email
UserAccess hosts IdentityServer-based OAuth2 password-grant authentication and JWT issuance. API authorization checks granular permissions rather than roles directly. Modules use a shared email abstraction; concrete delivery is infrastructure-owned.
• src/Modules/UserAccess/Infrastructure/IdentityServer/ResourceOwnerPasswordValidator.cs
• src/API/CompanyName.MyMeetings.API/Configuration/Authorization/AuthorizationChecker.cs
• src/BuildingBlocks/Infrastructure/Emails/EmailSender.cs
Persistence and Event Sourcing
MS SQL Server is divided into schemas per module, with EF Core repositories for conventional write models and SQL views for reads. Payments differs materially: aggregates are restored/appended through SQL Stream Store, then projectors maintain read tables from event streams and checkpoints.
• src/Database/CompanyName.MyMeetings.Database/Structure/meetings
• src/Modules/Payments/Infrastructure/AggregateStore/SqlStreamAggregateStore.cs
• src/Modules/Payments/Infrastructure/AggregateStore/SubscriptionsManager.cs
Database Lifecycle and Delivery
Database structure, migrations, seeds, and a DbUp migrator are versioned in-repo. NUKE is the build entry point and orchestrates compilation, tests, SQL Server-backed integration runs, migration, and SUT preparation. Key infrastructure is .NET 8, SQL Server, Docker Compose, Autofac, EF Core, Dapper, MediatR, Quartz, Serilog, IdentityServer4, and SQL Stream Store.
• src/Database/DatabaseMigrator/Program.cs
• src/Database/CompanyName.MyMeetings.Database/Scripts/Migrations
• build/Build.cs
Main entry points: Startup.cs API startup · meetings SQL Server · MeetingsController.cs Meetings controller · Meeting.cs Meeting aggregate
Components 21
API boundary
- ASP.NET Core API · Thin REST host and module composition boundary.
src/API/CompanyName.MyMeetings.API/Program.cs - API startup
src/API/CompanyName.MyMeetings.API/Startup.cs - Meetings controller
src/API/CompanyName.MyMeetings.API/Modules/Meetings/Meetings/MeetingsController.cs - Permission authorization · Enforces granular permissions at the API boundary.
src/API/CompanyName.MyMeetings.API/Configuration/Authorization/HasPermissionAuthorizationHandler.cs
Modules and processing
- Meetings module · Owns meeting groups, meetings, attendance, and comments.
src/Modules/Meetings/Infrastructure/MeetingsModule.cs - Administration module · Owns proposal approval.
src/Modules/Administration/Infrastructure/AdministrationModule.cs - Payments module · Owns subscriptions and fees.
src/Modules/Payments/Infrastructure/PaymentsModule.cs - IdentityServer authentication · Validates password grants and issues JWTs.
src/Modules/UserAccess/Infrastructure/IdentityServer/ResourceOwnerPasswordValidator.cs - Command pipeline · MediatR handlers apply validation, logging, and unit-of-work decorators.
src/Modules/Meetings/Application/Meetings/CreateMeeting/CreateMeetingCommandHandler.cs - Meeting aggregate · Enforces meeting business rules and raises domain events.
src/Modules/Meetings/Domain/Meetings/Meeting.cs - Meeting groups query · Reads module views directly with Dapper/SQL.
src/Modules/Meetings/Application/MeetingGroups/GetAllMeetingGroups/GetAllMeetingGroupsQueryHandler.cs - In-memory event bus · Publishes typed events between isolated modules.
src/BuildingBlocks/Infrastructure/EventBus/InMemoryEventBus.cs - Outbox processor · Dispatches persisted outgoing integration messages.
src/Modules/Meetings/Infrastructure/Configuration/Processing/Outbox/ProcessOutboxJob.cs - New user registered event
src/Modules/Registrations/IntegrationEvents/NewUserRegisteredIntegrationEvent.cs - Email sender · Concrete delivery behind the shared email abstraction.
src/BuildingBlocks/Infrastructure/Emails/EmailSender.cs
Persistence and delivery
- SQL Server · Module schemas, EF write stores, and SQL read views.
src/Database/CompanyName.MyMeetings.Database/Structure/meetings - Payments event store · Restores and appends payment aggregate streams.
src/Modules/Payments/Infrastructure/AggregateStore/SqlStreamAggregateStore.cs - Payments projections · Maintains payment read tables and checkpoints from streams.
src/Modules/Payments/Infrastructure/AggregateStore/SubscriptionsManager.cs - DbUp migrator · Applies versioned database migrations.
src/Database/DatabaseMigrator/Program.cs - Database migrations
src/Database/CompanyName.MyMeetings.Database/Scripts/Migrations - NUKE build · Builds, tests, migrates, and prepares integration runs.
build/Build.cs
Connections 21
- ASP.NET Core API → API startup: initializes
- API startup → Meetings module: composes
- API startup → Administration module: composes
- API startup → Payments module: composes
- Meetings controller → Permission authorization: authorizes with
- Meetings controller → Command pipeline: sends commands
- Meetings controller → Meeting groups query: sends queries
- Command pipeline → Meeting aggregate: loads and changes
- Meeting aggregate → SQL Server: persists via repository
- Meeting groups query → SQL Server: reads views
- Meeting aggregate → Outbox processor: records outgoing events
- Outbox processor → In-memory event bus: publishes
- In-memory event bus → New user registered event: delivers events
- In-memory event bus → Meetings module: consumed asynchronously
- IdentityServer authentication → ASP.NET Core API: issues JWTs for
- Payments module → Payments event store: stores aggregates in
- Payments event store → Payments projections: projects streams
- Payments projections → SQL Server: updates read tables
- Database migrations → DbUp migrator: applied by
- DbUp migrator → SQL Server: migrates
- NUKE build → DbUp migrator: orchestrates