|
| 1 | +# Copilot instructions for VerticalSliceArchitecture |
| 2 | + |
| 3 | +Use these project-specific guidelines to propose changes and generate code that fits this repo. |
| 4 | + |
| 5 | +## Big picture |
| 6 | + |
| 7 | +- Solution has 2 projects: |
| 8 | + - `src/Api` (ASP.NET Core entrypoint hosting DI, middleware, Swagger). Controllers live in the Application project. |
| 9 | + - `src/Application` (all features, domain, infrastructure, and shared concerns). Code is organized by vertical slices under `Features/**`. |
| 10 | +- Each feature keeps the HTTP endpoint, request/response types, validation, and MediatR handler together in one file. Example: `Features/TodoItems/CreateTodoItem.cs` contains: |
| 11 | + - `CreateTodoItemController : ApiControllerBase` with explicit route attribute |
| 12 | + - `record CreateTodoItemCommand : IRequest<ErrorOr<int>>` |
| 13 | + - `AbstractValidator<CreateTodoItemCommand>` (FluentValidation) |
| 14 | + - `IRequestHandler<,>` that uses `ApplicationDbContext` |
| 15 | +- Cross-cutting behaviors are provided via MediatR pipeline: `AuthorizationBehaviour`, `PerformanceBehaviour`, `ValidationBehaviour`. |
| 16 | +- Persistence via EF Core in `Infrastructure/Persistence/ApplicationDbContext.cs`. Domain events are collected and dispatched after `SaveChangesAsync`. |
| 17 | +- Default DB is in-memory; set `UseInMemoryDatabase=false` and configure `DefaultConnection` in `src/Api/appsettings.json` to use SQL Server. |
| 18 | + |
| 19 | +## Conventions and patterns |
| 20 | + |
| 21 | +- Add new endpoints under `src/Application/Features/<Feature>/<Action>.cs`. Keep controller, command/query, validator, handler, and DTOs together unless the file becomes unwieldy. |
| 22 | +- Controllers should inherit `ApiControllerBase` and typically use explicit absolute routes, e.g. `[HttpPost("/api/todo-items")]` or `[HttpGet("/api/todo-lists")]` (see existing features). |
| 23 | +- Handlers access data via `ApplicationDbContext` directly; no repository abstraction. Prefer `AsNoTracking()` for queries. |
| 24 | +- Return types use ErrorOr: commands return `ErrorOr<T>`, queries return `ErrorOr<VM>` and map entities to read models/records in the same file. |
| 25 | +- Add validators with `includeInternalTypes: true` (already wired). Validation errors flow via `ErrorOr` and `ApiControllerBase.Problem`. |
| 26 | +- Use domain events from entities (e.g., add `TodoItemCreatedEvent` to `entity.DomainEvents`). Handlers for domain events live under `Features/.../EventHandlers`. |
| 27 | + |
| 28 | +## Key files |
| 29 | + |
| 30 | +- `src/Api/Program.cs` – DI, middleware (Swagger, CORS any-origin, ProblemDetails), health checks. |
| 31 | +- `src/Application/ConfigureServices.cs` – registers MediatR + pipeline behaviors + validators; configures EF Core (in-memory or SQL Server). |
| 32 | +- `src/Application/Common/ApiControllerBase.cs` – Mediator access and consistent error handling using ErrorOr. |
| 33 | +- `src/Application/Infrastructure/Persistence/ApplicationDbContext.cs` – DbSets, auditing, domain event dispatch. |
| 34 | + |
| 35 | +## Developer workflows |
| 36 | + |
| 37 | +- Build: `dotnet build` (or VS Code task "build"). |
| 38 | +- Run API: `dotnet run --project src/Api/Api.csproj` (or task "watch"). Swagger UI is at `/`. |
| 39 | +- Tests: `dotnet test tests/Application.UnitTests/Application.UnitTests.csproj`; integration tests require a SQL instance. |
| 40 | +- EF Core migrations (from repo root): |
| 41 | + - Add: `dotnet ef migrations add <Name> --project src/Application --startup-project src/Api --output-dir Infrastructure/Persistence/Migrations` |
| 42 | + - Update DB: `dotnet ef database update --project src/Application --startup-project src/Api` |
| 43 | +- Formatting/analysis: `dotnet format` (or `--verify-no-changes`). |
| 44 | + |
| 45 | +## Integration points |
| 46 | + |
| 47 | +- Packages: MediatR, FluentValidation, ErrorOr, EF Core, Swashbuckle (Swagger). |
| 48 | +- Sample REST requests are under `requests/**` for quick manual testing. |
| 49 | + |
| 50 | +## When adding a new feature |
| 51 | + |
| 52 | +- Create a single file under `Features/<Area>/<VerbNoun>.cs` that mirrors the existing Todo examples. |
| 53 | +- Use explicit routes, ErrorOr results, validators, and `ApplicationDbContext`. Add domain events if applicable. |
| 54 | +- Keep controllers thin; push logic into the handler, and map entities to DTOs/VMs in the same file. |
0 commit comments