dotnet-backend

📁 erikarens/dotnet-agent-skill 📅 12 days ago
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

  1. Define request/response DTOs in Core/Dtos/, Core/Request/, or Core/Response/
  2. Create or extend a service in Core/Services/ (DTOs as params, not EF models)
  3. Add model-to-DTO mapping as extension methods for reusability
  4. Create validator using FluentValidation in Api/Validators/
  5. Add controller action with:
    • [FromServices] validator injection (not constructor)
    • CancellationToken parameter passed through the whole chain
    • ProducesResponseType<T>(StatusCodes.StatusXXX) annotations (use enum, not magic numbers)
    • No try-catch (ErrorHandlerMiddleware handles exceptions)
  6. 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 AppException with inner exception.
  • Validation: Throw InputException for 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