Skip to content

Commit 654978c

Browse files
committed
feat: add Copilot instructions for VerticalSliceArchitecture
1 parent c812e05 commit 654978c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

.github/copilot-instructions.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Vertical Slice Architecture example in .NET 9
22

3+
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/nadirbad/VerticalSliceArchitecture?utm_source=oss&utm_medium=github&utm_campaign=nadirbad%2FVerticalSliceArchitecture&labelColor=171717&color=FF570A&link=https%3A%2F%2Fcoderabbit.ai&label=CodeRabbit+Reviews)
4+
35
Check out my blog post [A Guide to Vertical Slice Architecture in C# .NET](https://nadirbad.dev/posts/vetical-slice-architecture-dotnet/) for more details about Vertical Slice Architecture.
46

57
This project is an experiment trying to create an API solution template that uses a Vertical Slice architecture style.
@@ -9,7 +11,7 @@ Vertical Slice architecture can be a starting point and can be evolved later whe
911

1012
> We can start simple (Transaction Script) and simply refactor to the patterns that emerges from code smells we see in the business logic. [Vertical slice architecture by Jimmy Bogard](https://jimmybogard.com/vertical-slice-architecture/).
1113
12-
# Give it a star ⭐
14+
## Give it a star ⭐
1315

1416
Loving it? Show your support by giving this project a star!
1517

0 commit comments

Comments
 (0)