Skip to content

hotnunstar/csharp-solid-template

Repository files navigation

SOLID Template - Clean Architecture .NET 8 Web API

A comprehensive, production-ready template for building scalable web APIs using Clean Architecture principles and SOLID design patterns. This template serves as a foundation for enterprise-grade applications with built-in best practices, comprehensive documentation, and real-world examples.

Architecture Overview

β”œβ”€β”€ Domain/          # πŸ›οΈ Core business logic and entities
β”œβ”€β”€ Application/     # 🎯 Use cases and business rules  
β”œβ”€β”€ Infrastructure/  # πŸ”§ Data access and external services
β”œβ”€β”€ Presentation/    # 🌐 API controllers and endpoints
└── docs/            # πŸ“š Comprehensive documentation

πŸš€ Quick Start

Prerequisites

  • .NET 8.0 SDK
  • SQL Server (optional - uses In-Memory database by default)
  • Visual Studio 2022 or VS Code

Get Started

# Clone the template
git clone <repository-url>
cd SOLID_Template

# Restore dependencies
dotnet restore

# Run the application
dotnet run

# Access the API
# - Base URL: https://localhost:7299
# - Swagger UI: https://localhost:7299/swagger
# - Health Checks: https://localhost:7299/health

πŸ“š Documentation

🎯 Core Guides

πŸ› οΈ Examples & Patterns

πŸ“‹ Reference

✨ Production Features

🎯 Core Features

  • βœ… Clean Architecture - Separation of concerns with dependency inversion
  • βœ… SOLID Principles - Maintainable and extensible code design
  • βœ… Entity Framework Core 8.0 - Code-first with migrations
  • βœ… AutoMapper - Object-to-object mapping
  • βœ… FluentValidation - Declarative validation rules
  • βœ… Swagger/OpenAPI - Interactive API documentation

πŸ›‘οΈ Production-Ready

  • βœ… Global Exception Handling - Centralized error management with ProblemDetails
  • βœ… Structured Logging - Comprehensive logging with Serilog
  • βœ… Health Checks - Application and database health monitoring
  • βœ… CORS Configuration - Cross-origin resource sharing
  • βœ… Response Compression - Performance optimization
  • βœ… Memory Caching - In-memory caching foundation
  • βœ… Environment Configuration - Development/Production settings

πŸ§ͺ Quality Assurance

  • βœ… Unit Testing - Comprehensive test coverage
  • βœ… Integration Testing - End-to-end API testing
  • βœ… Validation Testing - Input validation testing
  • βœ… Repository Testing - Data access testing

Sample Implementation

Person Management System

Complete CRUD operations demonstrating:

GET    /api/persons           # Get all persons
GET    /api/persons/{id}      # Get person by ID
POST   /api/persons           # Create new person
PUT    /api/persons/{id}      # Update person
DELETE /api/persons/{id}      # Delete person (soft delete)

Example Request/Response

# Create a new person
curl -X POST "https://localhost:7299/api/persons" \
     -H "Content-Type: application/json" \
     -d '{
       "firstName": "John",
       "lastName": "Doe", 
       "email": "john.doe@example.com",
       "dateOfBirth": "1990-01-15"
     }'
{
  "success": true,
  "message": "Person created successfully",
  "data": {
    "id": 1,
    "firstName": "John",
    "lastName": "Doe",
    "email": "john.doe@example.com",
    "fullName": "John Doe",
    "age": 34,
    "createdDate": "2024-01-15T10:30:00Z"
  }
}

Configuration

Database Configuration

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=SOLIDTemplateDB;Trusted_Connection=true;"
  },
  "UseInMemoryDatabase": false
}

Logging Configuration

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft.AspNetCore": "Warning"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      { "Name": "File", "Args": { "path": "logs/app-.txt", "rollingInterval": "Day" } }
    ]
  }
}

πŸ› οΈ Development Workflow

Adding New Features

  1. πŸ“‹ Plan - Define requirements and design
  2. πŸ›οΈ Domain - Create entities and business logic
  3. πŸ”§ Infrastructure - Implement data access
  4. 🎯 Application - Add services and DTOs
  5. 🌐 Presentation - Create API endpoints
  6. πŸ§ͺ Test - Write comprehensive tests
  7. πŸ“š Document - Update documentation

See Adding Features Guide for detailed steps.

Database Migrations

# Add new migration
dotnet ef migrations add MigrationName

# Update database
dotnet ef database update

# Remove last migration
dotnet ef migrations remove

Testing

# Run all tests
dotnet test

# Run with coverage
dotnet test --collect:"XPlat Code Coverage"

# Run specific category
dotnet test --filter Category=Unit

πŸ“Š Monitoring & Health

Health Checks

  • Application Health: /health
  • Database Health: Automatic EF Core integration
  • Custom Health Checks: Extensible health monitoring

Logging & Monitoring

  • Structured Logging: JSON-formatted logs with Serilog
  • Request Logging: HTTP request/response logging
  • Error Tracking: Centralized error handling and logging
  • Performance Metrics: Built-in ASP.NET Core metrics

πŸ›οΈ Architecture Benefits

🎯 Maintainability

  • Clear Separation of business logic, data access, and presentation
  • SOLID Principles ensure code is easy to modify and extend
  • Dependency Injection makes components loosely coupled and testable

πŸš€ Scalability

  • Clean Architecture allows independent scaling of layers
  • Repository Pattern enables easy data store changes
  • Service Layer provides centralized business logic

πŸ§ͺ Testability

  • Dependency Injection enables easy mocking
  • Interface-based Design allows test doubles
  • Separated Concerns enable focused unit tests

πŸ”§ Flexibility

  • Plugin Architecture through dependency injection
  • Strategy Patterns for algorithmic flexibility
  • Open/Closed Principle for safe extensions

Tech Stack

Core Framework

  • .NET 8.0 - Latest LTS framework with performance improvements
  • ASP.NET Core - High-performance web API framework
  • Entity Framework Core 8.0 - Modern ORM with advanced features

Libraries & Tools

  • AutoMapper 12.0 - Object-to-object mapping
  • FluentValidation 11.8 - Fluent validation rules
  • Serilog - Structured logging framework
  • Swashbuckle - OpenAPI/Swagger documentation
  • xUnit - Unit testing framework
  • FluentAssertions - Fluent assertion library

Development Tools

  • Visual Studio 2022 - Full IDE support
  • VS Code - Lightweight development
  • SQL Server - Production database
  • In-Memory Database - Development/testing

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes and add tests
  4. Ensure all tests pass: dotnet test
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

πŸ“„ License

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

πŸ™ Acknowledgments

  • Clean Architecture concepts by Robert C. Martin
  • SOLID Principles by Robert C. Martin
  • .NET Community for excellent libraries and frameworks
  • Contributors who help improve this template

Ready to build amazing APIs? Start with our Getting Started Guide πŸš€

About

SOLID template for C# web API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages