dotnet-backend
2
总安装量
2
周安装量
#62749
全站排名
安装命令
npx skills add https://github.com/erikarens/dotnet-agent-skill --skill dotnet-backend
Agent 安装分布
opencode
2
claude-code
2
github-copilot
2
codex
2
kimi-cli
2
gemini-cli
2
Skill 文档
ASP.NET Core Backend
Optimized for Layered Architecture â dependencies flow inward: Api â Core â Persistence.
Project Structure
src/
Api/
Attributes/
Authorization/
Controllers/
Extensions/
Http/
ResponseEnvelope # Wraps all responses
ResponseResultExecutor
ServiceExtensions.cs # DI registrations
Swagger/
ResponseEnvelopeOperationFilter
Validators/
Jobs/ # Quartz cron jobs
Core/
Configurations/
Dtos/
Request/
Response/
Enums/
Services/ # Business logic (DTOs in/out)
Persistence/
Enums/
Models/ # EF Core entities
Migrations/
Migrations/ # EF Core migration files
Scripts/ # SQL scripts (same name as migration, .sql ext)
Key Workflows
Creating a New Endpoint
- Define request/response DTOs in
Core/Dtos/,Core/Request/, orCore/Response/ - Create or extend a service in
Core/Services/(DTOs as params, not EF models) - Add model-to-DTO mapping as extension methods for reusability
- Create validator using FluentValidation in
Api/Validators/ - Add controller action with:
[FromServices]validator injection (not constructor)CancellationTokenparameter passed through the whole chainProducesResponseType<T>(StatusCodes.StatusXXX)annotations (use enum, not magic numbers)- No try-catch (ErrorHandlerMiddleware handles exceptions)
- Register new services in
Api/Extensions/ServiceExtensions.cs
Database Migrations
Never create migration files manually. Use the bundled script:
./scripts/generate-migration.sh <MigrationName>
The script runs dotnet ef migrations add and generates a matching SQL script automatically. It auto-detects the src/ directory. Override with env vars if needed:
| Variable | Default |
|---|---|
SRC_DIR |
auto-detected |
STARTUP_PROJECT |
src/Api |
MIGRATIONS_PROJECT |
src/Migrations |
DB_CONTEXT |
auto-detected |
Pass --context <Name> to specify a DbContext explicitly.
Error Handling Pattern
- Controllers: No try-catch. ErrorHandlerMiddleware handles everything.
- Services: Catch unexpected exceptions, log, re-throw as
AppExceptionwith inner exception. - Validation: Throw
InputExceptionfor invalid input. Validators injected via[FromServices].
Response Envelope
All responses are wrapped in ResponseEnvelope<T> by middleware. Controllers return unwrapped types. The ResponseEnvelopeOperationFilter embeds the envelope in Swagger output so auto-generated clients work without patching.
Detailed References
- Coding guidelines (style, naming, error handling, DI, testing, security): See references/coding-guidelines.md
- API design (controllers, response types, Swagger, versioning): See references/api-design.md