- Enhanced Mermaid diagrams for better visual clarity and consistency across guides. - Added new sections on caching, data consistency, and observability patterns to the architecture documentation. - Improved formatting and structure to align with the English version, ensuring a cohesive user experience. - Removed outdated service communication documentation to reduce clutter and focus on relevant content.
280 lines
7.5 KiB
Markdown
280 lines
7.5 KiB
Markdown
# .NET 10 Microservice Template
|
|
|
|
> Enterprise-grade .NET 10 microservice template following DDD, CQRS, and Clean Architecture patterns.
|
|
|
|
## Overview
|
|
|
|
This template provides a production-ready structure for .NET microservices based on the eShopOnContainers reference architecture with:
|
|
|
|
- **Domain-Driven Design (DDD)** - Aggregates, Entities, Value Objects, Domain Events
|
|
- **CQRS Pattern** - Separate Commands (write) and Queries (read) with MediatR
|
|
- **Clean Architecture** - Domain, Infrastructure, API layered separation
|
|
- **EF Core 10** - PostgreSQL with connection resilience
|
|
- **FluentValidation** - Request validation
|
|
- **API Versioning** - URL segment versioning
|
|
- **Health Checks** - Kubernetes-ready probes
|
|
- **Structured Logging** - Serilog with console and Seq
|
|
|
|
## Prerequisites
|
|
|
|
| Requirement | Version |
|
|
|-------------|---------|
|
|
| .NET SDK | 10.0.101+ |
|
|
| Docker | 24.0+ |
|
|
| PostgreSQL | 15+ (or use Docker) |
|
|
|
|
## Quick Start
|
|
|
|
### 1. Create New Service
|
|
|
|
```bash
|
|
# Copy template to new service
|
|
cp -r services/_template_dot_net services/your-service-name
|
|
cd services/your-service-name
|
|
|
|
# Rename all occurrences of "MyService" to "YourService"
|
|
find . -type f -name "*.cs" -exec sed -i '' 's/MyService/YourService/g' {} +
|
|
find . -type f -name "*.csproj" -exec sed -i '' 's/MyService/YourService/g' {} +
|
|
```
|
|
|
|
### 2. Configure Environment
|
|
|
|
```bash
|
|
cp .env.example .env
|
|
nano .env
|
|
```
|
|
|
|
### 3. Run with Docker
|
|
|
|
```bash
|
|
docker-compose up -d
|
|
docker-compose logs -f myservice-api
|
|
```
|
|
|
|
### 4. Run Locally
|
|
|
|
```bash
|
|
dotnet restore
|
|
dotnet build
|
|
dotnet run --project src/MyService.API
|
|
```
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
_template_dot_net/
|
|
├── src/
|
|
│ ├── MyService.API/ # Presentation Layer (Controllers, CQRS)
|
|
│ │ ├── Controllers/ # API endpoints
|
|
│ │ ├── Application/ # CQRS Implementation
|
|
│ │ │ ├── Commands/ # Write operations (MediatR)
|
|
│ │ │ ├── Queries/ # Read operations
|
|
│ │ │ ├── Behaviors/ # MediatR pipeline behaviors
|
|
│ │ │ └── Validations/ # FluentValidation validators
|
|
│ │ └── Program.cs # Application entry point
|
|
│ │
|
|
│ ├── MyService.Domain/ # Domain Layer (Pure business logic)
|
|
│ │ ├── AggregatesModel/ # Aggregate roots and entities
|
|
│ │ ├── Events/ # Domain events
|
|
│ │ ├── Exceptions/ # Domain exceptions
|
|
│ │ └── SeedWork/ # Base classes
|
|
│ │
|
|
│ └── MyService.Infrastructure/ # Infrastructure Layer
|
|
│ ├── EntityConfigurations/ # EF Core configurations
|
|
│ ├── Repositories/ # Repository implementations
|
|
│ └── MyServiceContext.cs # DbContext with Unit of Work
|
|
│
|
|
├── tests/
|
|
│ ├── MyService.UnitTests/ # Unit tests
|
|
│ └── MyService.FunctionalTests/ # Integration tests
|
|
│
|
|
├── Dockerfile # Multi-stage Docker build
|
|
└── docker-compose.yml # Local development setup
|
|
```
|
|
|
|
## Architecture
|
|
|
|
```mermaid
|
|
graph TB
|
|
subgraph "API Layer"
|
|
C[Controllers]
|
|
CMD[Commands]
|
|
Q[Queries]
|
|
B[Behaviors]
|
|
V[Validations]
|
|
end
|
|
|
|
subgraph "Domain Layer"
|
|
AR[Aggregate Roots]
|
|
E[Entities]
|
|
VO[Value Objects]
|
|
DE[Domain Events]
|
|
end
|
|
|
|
subgraph "Infrastructure Layer"
|
|
DB[(PostgreSQL)]
|
|
R[Repositories]
|
|
CTX[DbContext]
|
|
end
|
|
|
|
C --> CMD
|
|
C --> Q
|
|
CMD --> B --> V
|
|
CMD --> AR
|
|
Q --> R
|
|
R --> CTX --> DB
|
|
|
|
style C fill:#4a90d9,stroke:#2d5986,color:#fff
|
|
style AR fill:#50c878,stroke:#2d8659,color:#fff
|
|
style DB fill:#ff6b6b,stroke:#c0392b,color:#fff
|
|
```
|
|
|
|
## Layer Responsibilities
|
|
|
|
### 1. Domain Layer
|
|
|
|
- **ZERO** external dependencies (except MediatR.Contracts)
|
|
- Contains only POCO classes
|
|
- Implements DDD tactical patterns
|
|
|
|
| Component | Purpose |
|
|
|-----------|---------|
|
|
| **SeedWork** | Base classes: Entity, ValueObject, IAggregateRoot |
|
|
| **AggregatesModel** | Aggregate roots with entities and value objects |
|
|
| **Events** | Domain events for cross-aggregate communication |
|
|
| **Exceptions** | Domain-specific exceptions |
|
|
|
|
### 2. Infrastructure Layer
|
|
|
|
- Database access (EF Core)
|
|
- Repository implementations
|
|
- External service integrations
|
|
|
|
### 3. API Layer
|
|
|
|
- Controllers for HTTP handling
|
|
- Commands for write operations
|
|
- Queries for read operations
|
|
- MediatR behaviors for cross-cutting concerns
|
|
|
|
## CQRS Pattern
|
|
|
|
### Commands (Write Operations)
|
|
|
|
```csharp
|
|
public record CreateSampleCommand(string Name, string? Description)
|
|
: IRequest<CreateSampleCommandResult>;
|
|
|
|
public class CreateSampleCommandHandler : IRequestHandler<CreateSampleCommand, CreateSampleCommandResult>
|
|
{
|
|
public async Task<CreateSampleCommandResult> Handle(CreateSampleCommand request, CancellationToken ct)
|
|
{
|
|
var sample = new Sample(request.Name, request.Description);
|
|
_repository.Add(sample);
|
|
await _repository.UnitOfWork.SaveEntitiesAsync(ct);
|
|
return new CreateSampleCommandResult(sample.Id);
|
|
}
|
|
}
|
|
```
|
|
|
|
### MediatR Pipeline
|
|
|
|
```
|
|
Request → LoggingBehavior → ValidatorBehavior → TransactionBehavior → Handler → Response
|
|
```
|
|
|
|
## API Endpoints
|
|
|
|
| Method | Endpoint | Description |
|
|
|--------|----------|-------------|
|
|
| `GET` | `/api/v1/samples` | Get all samples |
|
|
| `GET` | `/api/v1/samples/{id}` | Get sample by ID |
|
|
| `POST` | `/api/v1/samples` | Create new sample |
|
|
| `PUT` | `/api/v1/samples/{id}` | Update sample |
|
|
| `DELETE` | `/api/v1/samples/{id}` | Delete sample |
|
|
| `PATCH` | `/api/v1/samples/{id}/status` | Change status |
|
|
|
|
### Health Endpoints
|
|
|
|
| Endpoint | Purpose |
|
|
|----------|---------|
|
|
| `/health` | Full health status |
|
|
| `/health/live` | Liveness probe |
|
|
| `/health/ready` | Readiness probe |
|
|
|
|
## Environment Variables
|
|
|
|
| Variable | Description | Default |
|
|
|----------|-------------|---------|
|
|
| `ASPNETCORE_ENVIRONMENT` | Environment name | `Development` |
|
|
| `DATABASE_URL` | PostgreSQL connection | - |
|
|
| `REDIS_URL` | Redis connection | - |
|
|
| `JWT_SECRET` | JWT signing secret | - |
|
|
|
|
## Testing
|
|
|
|
```bash
|
|
# Run all tests
|
|
dotnet test
|
|
|
|
# Run with coverage
|
|
dotnet test /p:CollectCoverage=true /p:CoverageReportFormat=cobertura
|
|
|
|
# Run specific test project
|
|
dotnet test tests/MyService.UnitTests
|
|
```
|
|
|
|
## Deployment
|
|
|
|
### Docker Build
|
|
|
|
```bash
|
|
docker build -t myservice:latest .
|
|
docker run -p 5000:8080 --env-file .env myservice:latest
|
|
```
|
|
|
|
### Kubernetes
|
|
|
|
```yaml
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: myservice-api
|
|
spec:
|
|
replicas: 3
|
|
template:
|
|
spec:
|
|
containers:
|
|
- name: api
|
|
image: myservice:latest
|
|
livenessProbe:
|
|
httpGet:
|
|
path: /health/live
|
|
port: 8080
|
|
readinessProbe:
|
|
httpGet:
|
|
path: /health/ready
|
|
port: 8080
|
|
```
|
|
|
|
## What's New in .NET 10
|
|
|
|
- **C# 14** language features
|
|
- Improved **Native AOT** support
|
|
- Better **async/await** performance
|
|
- Enhanced **JSON serialization**
|
|
- 3-year **LTS** support (until November 2028)
|
|
|
|
## Related Documentation
|
|
|
|
- [Architecture Details](../architecture/microservices-communication.md)
|
|
- [DDD Patterns](../architecture/system-design.md)
|
|
- [Deployment Guide](../guides/deployment.md)
|
|
|
|
## Resources
|
|
|
|
- [eShopOnContainers](https://github.com/dotnet-architecture/eShopOnContainers)
|
|
- [.NET 10 Documentation](https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-10)
|
|
- [MediatR](https://github.com/jbogard/MediatR)
|
|
- [FluentValidation](https://docs.fluentvalidation.net/)
|