|
| 1 | +# GitHub Copilot Instructions for Clean Architecture Template |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +This is a **Clean Architecture template** for .NET 9 that demonstrates Domain-Driven Design (DDD) patterns. It's a starter template, not a reference application - delete sample code once you understand the patterns. |
| 5 | + |
| 6 | +## Architecture & Project Structure |
| 7 | + |
| 8 | +### Core Dependencies Flow |
| 9 | +- **Core** ← UseCases ← Infrastructure |
| 10 | +- **Core** ← UseCases ← Web |
| 11 | +- Never allow Core to depend on outer layers |
| 12 | + |
| 13 | +### Key Projects |
| 14 | +- **Core**: Domain entities, aggregates, value objects, specifications, interfaces |
| 15 | +- **UseCases**: Commands/queries (CQRS), MediatR handlers, application logic |
| 16 | +- **Infrastructure**: EF Core, external services, email, file access |
| 17 | +- **Web**: FastEndpoints API, REPR pattern, validation |
| 18 | + |
| 19 | +## Development Patterns |
| 20 | + |
| 21 | +### API Endpoints (FastEndpoints + REPR) |
| 22 | +- One endpoint per file: `Create.cs`, `Update.cs`, `Delete.cs`, `GetById.cs` |
| 23 | +- Separate request/response/validator files: `Create.CreateRequest.cs`, `Create.CreateValidator.cs` |
| 24 | +- Use `Endpoint<TRequest, TResponse>` base class |
| 25 | +- Example: `src/Clean.Architecture.Web/Contributors/Create.cs` |
| 26 | + |
| 27 | +### Domain Model (Core) |
| 28 | +- Entities use encapsulation - minimize public setters |
| 29 | +- Group related entities into Aggregates |
| 30 | +- Use Value Objects (e.g., `ContributorName.From()`) |
| 31 | +- Domain Events for cross-aggregate communication |
| 32 | +- Repository interfaces defined in Core, implemented in Infrastructure |
| 33 | + |
| 34 | +### Use Cases (CQRS) |
| 35 | +- Commands for mutations, Queries for reads |
| 36 | +- Queries can bypass repository pattern for performance |
| 37 | +- Use MediatR for command/query handling |
| 38 | +- Chain of responsibility for cross-cutting concerns (logging, validation) |
| 39 | + |
| 40 | +### Validation Strategy |
| 41 | +- **API Level**: FluentValidation on request DTOs (FastEndpoints integration) |
| 42 | +- **Use Case Level**: Validate commands/queries (defensive coding) |
| 43 | +- **Domain Level**: Business invariants throw exceptions, assume pre-validated input |
| 44 | + |
| 45 | +## Essential Commands |
| 46 | + |
| 47 | +### Build & Test |
| 48 | +```bash |
| 49 | +dotnet build Clean.Architecture.sln |
| 50 | +dotnet test Clean.Architecture.sln |
| 51 | +``` |
| 52 | + |
| 53 | +### Entity Framework Migrations |
| 54 | +```bash |
| 55 | +# From Web project directory |
| 56 | +dotnet ef migrations add MigrationName -c AppDbContext -p ../Clean.Architecture.Infrastructure/Clean.Architecture.Infrastructure.csproj -s Clean.Architecture.Web.csproj -o Data/Migrations |
| 57 | + |
| 58 | +dotnet ef database update -c AppDbContext -p ../Clean.Architecture.Infrastructure/Clean.Architecture.Infrastructure.csproj -s Clean.Architecture.Web.csproj |
| 59 | +``` |
| 60 | + |
| 61 | +### Template Installation & Usage |
| 62 | +```bash |
| 63 | +dotnet new install Ardalis.CleanArchitecture.Template |
| 64 | +dotnet new clean-arch -o Your.ProjectName |
| 65 | +``` |
| 66 | + |
| 67 | +## Key Dependencies & Patterns |
| 68 | + |
| 69 | +### Primary Libraries |
| 70 | +- **FastEndpoints**: API endpoints (replaced Controllers/Minimal APIs) |
| 71 | +- **MediatR**: Command/query handling in UseCases |
| 72 | +- **EF Core**: Data access (SQLite default, easily changed to SQL Server) |
| 73 | +- **Ardalis.Specification**: Repository query specifications |
| 74 | +- **Ardalis.Result**: Error handling pattern |
| 75 | +- **Serilog**: Structured logging |
| 76 | + |
| 77 | +### Central Package Management |
| 78 | +- All package versions in `Directory.Packages.props` |
| 79 | +- Use `<PackageReference Include="..." />` without Version attribute |
| 80 | + |
| 81 | +### Test Organization |
| 82 | +- **UnitTests**: Core business logic, use cases |
| 83 | +- **IntegrationTests**: Database, infrastructure components |
| 84 | +- **FunctionalTests**: API endpoints (subcutaneous testing) |
| 85 | +- Use `Microsoft.AspNetCore.Mvc.Testing` for API tests |
| 86 | + |
| 87 | +## File Organization Conventions |
| 88 | + |
| 89 | +### Web Project Structure |
| 90 | +``` |
| 91 | +Contributors/ |
| 92 | + Create.cs # Endpoint |
| 93 | + Create.CreateRequest.cs # Request DTO |
| 94 | + Create.CreateResponse.cs # Response DTO |
| 95 | + Create.CreateValidator.cs # FluentValidation |
| 96 | + Update.cs, Delete.cs, etc. |
| 97 | +``` |
| 98 | + |
| 99 | +### Sample vs Template |
| 100 | +- `/sample` folder: Complete working example (NimblePros.SampleToDo) |
| 101 | +- `/src` folder: Clean template ready for your project |
| 102 | +- Study sample for patterns, use src for new projects |
| 103 | + |
| 104 | +## Common Gotchas |
| 105 | + |
| 106 | +- Don't include hyphens in project names (template limitation) |
| 107 | +- Replace `Ardalis.SharedKernel` with your own shared kernel |
| 108 | +- Database path in `appsettings.json` for SQLite |
| 109 | +- Use absolute paths in EF migration commands |
| 110 | +- FastEndpoints uses different validation approach than Controller-based APIs |
| 111 | + |
| 112 | +## VS Code Tasks |
| 113 | +Use the predefined tasks: `build`, `publish`, `watch` instead of manual `dotnet` commands when possible. |
0 commit comments