Skip to content

A forum application built with .NET 10.0 using microservices architecture, implementing Domain-Driven Design (DDD) principles, CQRS pattern, and event-driven communication.

Notifications You must be signed in to change notification settings

fsmaiorano/forum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

88 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Forum API - Microservices Architecture

A modern forum application built with .NET 10.0 using microservices architecture, implementing Domain-Driven Design (DDD) principles, CQRS pattern, and event-driven communication.

πŸ“‹ Overview

This project is a distributed forum system composed of three main microservices that handle different aspects of the application:

  • Forum Service: Manages questions, answers, and forum-related operations
  • User Service: Handles authentication, authorization, and user management
  • Notification Service: Processes and delivers notifications to users

πŸ—οΈ Architecture

The project follows a microservices architecture with the following characteristics:

  • Domain-Driven Design (DDD): Each service is organized around business domains
  • Clean Architecture: Clear separation of concerns with distinct layers (Domain, Application, Infrastructure, Endpoints)
  • Event-Driven Communication: Microservices communicate through integration events
  • CQRS Pattern: Separation of read and write operations
  • Minimal API: Using .NET's minimal API approach for lightweight endpoints

Building Blocks

The project includes a shared BuildingBlocks library that provides:

  • Base Domain Classes: Aggregate, Entity, ValueObject, UniqueEntityId, WatchedList
  • Event Bus: Implementation for both in-memory and RabbitMQ-based event publishing
  • Custom Exceptions: Domain-specific exception handling (BadRequest, NotFound, Validation, etc.)
  • Middleware: Correlation ID tracking, Request/Response logging
  • Logging: Structured logging with Serilog
  • Pagination: Generic pagination support
  • Result Pattern: Functional error handling

πŸš€ Technologies

Core Framework

  • .NET 10.0 - Latest .NET framework
  • C# 13 - Modern C# features
  • ASP.NET Core - Web framework with Minimal APIs

Databases

  • PostgreSQL - Primary database for all services
  • Entity Framework Core 10.0 - ORM for database access
  • Npgsql - PostgreSQL provider for EF Core

Message Broker

  • RabbitMQ - Message broker for asynchronous communication between services
  • RabbitMQ.Client 6.8.1 - RabbitMQ client library

Authentication & Security

  • ASP.NET Core Identity - User management framework
  • JWT Bearer Authentication - Token-based authentication
  • Microsoft.IdentityModel.Tokens - JWT token handling

Logging & Monitoring

  • Serilog - Structured logging
  • Serilog.AspNetCore - ASP.NET Core integration
  • Serilog.Sinks.Console - Console output
  • Serilog.Sinks.File - File-based logging
  • Serilog.Expressions - Advanced logging expressions

API Documentation

  • Swagger/OpenAPI - API documentation and testing interface
  • Swashbuckle.AspNetCore - Swagger generator for ASP.NET Core

DevOps & Containerization

  • Docker - Container platform
  • Docker Compose - Multi-container orchestration

Testing

  • xUnit - Testing framework
  • In-Memory Database - For unit and integration testing

πŸ“Š Database Schema

Each microservice has its own dedicated database:

  • ForumDb: Stores questions, answers, attachments, and forum-related data
  • UserDb: Manages user accounts, roles, and authentication data
  • NotificationDb: Handles notification records and delivery status

πŸ”Œ Port Mapping

Microservices

Service Local HTTP Local HTTPS Docker HTTP Docker HTTPS Docker Internal
Forum 5000 5050 8000 8443 8080 - 8081
Notification 5001 5051 8001 8444 8080 - 8081
User 5002 5052 8002 8445 8080 - 8081

Note: Docker uses safe ports (8000-8002, 8443-8445) to avoid browser blocking issues. Ports 6000-6007 are considered unsafe by modern browsers.

Infrastructure

Service Port Management Port
ForumDb 5433 -
NotificationDb 5434 -
UserDb 5435 -
RabbitMQ 5672 15672

🎯 Features

Forum Service

  • Create, read, update, and delete questions
  • Create, read, update, and delete answers
  • Mark best answer for questions
  • Filter questions by author
  • Attachment management
  • Student and Instructor roles
  • Pagination support

User Service

  • User registration
  • User authentication with JWT tokens
  • Role-based authorization
  • Password management
  • Email validation
  • Token refresh mechanism

Notification Service

  • Send notifications to users
  • Read notification status
  • Integration with other services via events
  • Notification history

πŸ“‚ Project Structure

Forum/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ BuildingBlocks/          # Shared library
β”‚   β”‚   β”œβ”€β”€ Base/                # Domain base classes
β”‚   β”‚   β”œβ”€β”€ Exceptions/          # Custom exceptions
β”‚   β”‚   β”œβ”€β”€ Extensions/          # Helper extensions
β”‚   β”‚   β”œβ”€β”€ Logging/             # Logging infrastructure
β”‚   β”‚   β”œβ”€β”€ Messaging/           # Event bus implementation
β”‚   β”‚   └── Middleware/          # Common middleware
β”‚   β”œβ”€β”€ Forum/                   # Forum microservice
β”‚   β”‚   β”œβ”€β”€ Application/         # Application layer
β”‚   β”‚   β”œβ”€β”€ Domain/              # Domain layer
β”‚   β”‚   β”œβ”€β”€ Endpoints/           # API endpoints
β”‚   β”‚   β”œβ”€β”€ Infrastructure/      # Infrastructure layer
β”‚   β”‚   └── Migrations/          # Database migrations
β”‚   β”œβ”€β”€ Notification/            # Notification microservice
β”‚   β”‚   β”œβ”€β”€ Application/
β”‚   β”‚   β”œβ”€β”€ Domain/
β”‚   β”‚   β”œβ”€β”€ Endpoints/
β”‚   β”‚   β”œβ”€β”€ Infrastructure/
β”‚   β”‚   └── Migrations/
β”‚   └── User/                    # User microservice
β”‚       β”œβ”€β”€ Data/                # Data layer
β”‚       β”œβ”€β”€ DTOs/                # Data transfer objects
β”‚       β”œβ”€β”€ Endpoints/           # API endpoints
β”‚       β”œβ”€β”€ Extensions/          # Extensions
β”‚       β”œβ”€β”€ Migrations/          # Database migrations
β”‚       └── Services/            # Business services
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ Forum.UnitTest/          # Forum unit tests
β”‚   β”œβ”€β”€ Notification.UnitTest/   # Notification unit tests
β”‚   β”œβ”€β”€ User.UnitTest/           # User unit tests
β”‚   └── Seed/                    # Database seeding
β”œβ”€β”€ docs/                        # Documentation
β”œβ”€β”€ compose.yaml                 # Docker Compose configuration
└── Forum.sln                    # Solution file

🚦 Getting Started

Prerequisites

  • .NET 10.0 SDK
  • Docker and Docker Compose
  • PostgreSQL (if running locally without Docker)
  • RabbitMQ (if running locally without Docker)

Running with Docker Compose

  1. Clone the repository:
git clone <repository-url>
cd Forum
  1. Option A - Run Everything in Docker (Recommended)
# Start all services (APIs + Databases + RabbitMQ)
docker-compose up -d

# Access the APIs:
# Forum API:        http://localhost:8000/swagger
# Notification API: http://localhost:8001/swagger
# User API:         http://localhost:8002/swagger

# HTTPS is also available:
# Forum API:        https://localhost:8443/swagger
# Notification API: https://localhost:8444/swagger
# User API:         https://localhost:8445/swagger
  1. Option B - Run APIs Locally (For Debugging)
# Start only infrastructure (databases and RabbitMQ)
docker-compose up -d userdb forumdb notificationdb messagebroker

# Terminal 1 - Forum Service
cd src/Forum
dotnet run

# Terminal 2 - User Service
cd src/User
dotnet run

# Terminal 3 - Notification Service
cd src/Notification
dotnet run

# Access the APIs:
# Forum API:        http://localhost:5000/swagger
# Notification API: http://localhost:5001/swagger
# User API:         http://localhost:5002/swagger

HTTPS Certificates in Docker

HTTPS certificates are automatically generated inside containers on first startup. No manual configuration needed!

Database Migrations

Migrations run automatically when starting services. To run manually:

# Forum Service
cd src/Forum
dotnet ef database update

# User Service
cd src/User
dotnet ef database update

# Notification Service
cd src/Notification
dotnet ef database update

πŸ“– API Documentation

Local Development

Docker Environment

HTTP:

HTTPS:

RabbitMQ Management

πŸ§ͺ Testing

Run all tests:

dotnet test

Run tests for a specific project:

dotnet test tests/Forum.UnitTest
dotnet test tests/User.UnitTest
dotnet test tests/Notification.UnitTest

πŸ”§ Configuration

Each service can be configured via appsettings.json and appsettings.Development.json. Key configurations include:

  • Connection Strings: Database connections
  • JWT Settings: Token configuration (User service)
  • RabbitMQ Settings: Message broker configuration
  • Logging Levels: Serilog configuration
  • CORS Policies: Cross-origin resource sharing

πŸ“ Domain Models

Forum Service

  • Question: Forum questions with title, content, and metadata
  • Answer: Responses to questions
  • Attachment: File attachments for questions/answers
  • Student: Student user type
  • Instructor: Instructor user type with additional privileges

User Service

  • ApplicationUser: Extended Identity user
  • Roles: Role-based access control

Notification Service

  • Notification: User notifications with read status

πŸ”„ Event-Driven Communication

The system uses an event bus pattern for inter-service communication:

  • Domain Events: Events within a single service boundary
  • Integration Events: Events that cross service boundaries
  • Event Handlers: Process events asynchronously

Supported event bus implementations:

  • InMemoryEventBus: For development and testing
  • RabbitMQEventBus: For production use

πŸ›‘οΈ Error Handling

The application implements comprehensive error handling with:

  • Custom Exception Types: BadRequest, NotFound, Validation, Forbidden, InternalServer
  • Global Exception Handler: Centralized exception processing
  • Result Pattern: Functional error handling for operations
  • Structured Logging: All errors are logged with context

🐳 Docker

Quick Commands

# Start everything
docker-compose up -d

# View logs
docker-compose logs -f

# View specific service logs
docker-compose logs -f forum
docker-compose logs -f notification
docker-compose logs -f user

# Check status
docker-compose ps

# Stop everything
docker-compose down

# Stop and remove volumes (clean slate)
docker-compose down -v

# Restart a specific service
docker-compose restart forum

Docker Features

βœ… Automatic HTTPS Certificates - Generated automatically inside containers
βœ… Safe Ports - Uses browser-safe ports (8000-8002, 8443-8445)
βœ… Health Checks - All services have health monitoring
βœ… Persistent Data - Databases use volumes for data persistence
βœ… Auto-restart - Services restart automatically on failure
βœ… Zero Configuration - Just run docker-compose up -d

Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Host Machine                         β”‚
β”‚                                                             β”‚
β”‚  Browser β†’ :8000 (Forum HTTP)    β†’ :8443 (Forum HTTPS)     β”‚
β”‚         β†’ :8001 (Notification)   β†’ :8444 (Notification)     β”‚
β”‚         β†’ :8002 (User)          β†’ :8445 (User)             β”‚
β”‚         β†’ :15672 (RabbitMQ UI)                             β”‚
β”‚                                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚              Docker Network                          β”‚  β”‚
β”‚  β”‚                                                      β”‚  β”‚
β”‚  β”‚  ┏━━━━━━━━━━┓  ┏━━━━━━━━━━━━━┓  ┏━━━━━━━━━━┓       β”‚  β”‚
β”‚  β”‚  ┃ Forum    ┃  ┃ Notification┃  ┃  User    ┃       β”‚  β”‚
β”‚  β”‚  ┃   API    ┃  ┃     API     ┃  ┃   API    ┃       β”‚  β”‚
β”‚  β”‚  ┗━━━━━━━━━━┛  ┗━━━━━━━━━━━━━┛  ┗━━━━━━━━━━┛       β”‚  β”‚
β”‚  β”‚       ↓              ↓                ↓              β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”          β”‚  β”‚
β”‚  β”‚  β”‚ForumDB β”‚    β”‚NotifyDBβ”‚      β”‚UserDB  β”‚          β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β”‚  β”‚
β”‚  β”‚       ↓              ↓                ↓              β”‚  β”‚
β”‚  β”‚            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     β”‚  β”‚
β”‚  β”‚            β”‚    RabbitMQ       β”‚                     β”‚  β”‚
β”‚  β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                     β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Troubleshooting

APIs not responding?

# Check if containers are running
docker-compose ps

# Check logs for errors
docker-compose logs -f

Database connection issues?

# Restart databases
docker-compose restart forumdb notificationdb userdb

RabbitMQ issues?

# Restart RabbitMQ
docker-compose restart messagebroker

Clean everything and start fresh?

docker-compose down -v
docker-compose up -d --build

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built with .NET 10.0
  • Inspired by Clean Architecture and DDD principles
  • Microservices architecture patterns

About

A forum application built with .NET 10.0 using microservices architecture, implementing Domain-Driven Design (DDD) principles, CQRS pattern, and event-driven communication.

Resources

Stars

Watchers

Forks

Languages