File contents .NET Idioms and Patterns
.NET 8+ rewards minimal APIs, DI, and async-first design. Idiomatic .NET = clean, performant, cloud-native.
Scope: .NET framework patterns. For C#: @.gemini/skills/csharp-idioms/SKILL.md.
Minimal APIs
Minimal APIs for simple endpoints:
app.MapGet("/api/tasks/{id}", async (string id, ITaskService service) =>
await service.GetByIdAsync(id) is Task task
? Results.Ok(task)
: Results.NotFound());
app.MapPost("/api/tasks", async (CreateTaskRequest request, ITaskService service) =>
{
var task = await service.CreateAsync(request);
return Results.Created($"/api/tasks/{task.Id}", task);
});
Controllers for complex endpoints with filters, model binding, etc.
Entity Framework Core
DbContext per request (scoped lifetime).
Migrations via CLI: dotnet ef migrations add AddTaskPriority
dotnet ef database update
AsNoTracking() for read-only queries.
Compiled queries for hot paths.
Configuration
IOptions<T> pattern for strongly-typed config:
builder.Services.Configure<DatabaseOptions>(builder.Configuration.GetSection("Database"));
User secrets for local development, Key Vault for production.
Middleware
Custom middleware for cross-cutting concerns (logging, correlation IDs).
UseExceptionHandler for global error handling.
Testing
xUnit + FluentAssertions:
[Fact]
public async Task GetTask_ReturnsOk_WhenExists()
{
var client = _factory.CreateClient();
var response = await client.GetAsync("/api/tasks/1");
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
WebApplicationFactory<T> for integration tests.
Respawn for database cleanup.
Formatting and Static Analysis
Tool
Purpose
Command
dotnet format
Formatting
dotnet format
Roslyn analyzers
Analysis
Built-in
dotnet list package --vulnerable
CVE scanning
Built-in
Related
C# Idioms @.gemini/skills/csharp-idioms/SKILL.md
API Design Principles @.gemini/skills/api-design-principles/SKILL.md
Database Design Principles @.gemini/skills/database-design-principles/SKILL.md
1 --- 2 name: dotnet-idioms 3 description: .NET Idioms and Patterns 4 --- 5 6 ## .NET Idioms and Patterns 7 8 .NET 8+ rewards minimal APIs, DI, and async-first design. Idiomatic .NET = clean, performant, cloud-native. 9 10 > Scope: .NET framework patterns. For C#: `@.gemini/skills/csharp-idioms/SKILL.md`. 11 12 ### Minimal APIs 13 14 1. **Minimal APIs for simple endpoints:** 15 ```csharp 16 app.MapGet("/api/tasks/{id}", async (string id, ITaskService service) => 17 await service.GetByIdAsync(id) is Task task 18 ? Results.Ok(task) 19 : Results.NotFound()); 20 21 app.MapPost("/api/tasks", async (CreateTaskRequest request, ITaskService service) => 22 { 23 var task = await service.CreateAsync(request); 24 return Results.Created($"/api/tasks/{task.Id}", task); 25 }); 26 ``` 27 28 2. **Controllers for complex endpoints** with filters, model binding, etc. 29 30 ### Entity Framework Core 31 32 1. **DbContext per request** (scoped lifetime). 33 2. **Migrations via CLI:** 34 ```bash 35 dotnet ef migrations add AddTaskPriority 36 dotnet ef database update 37 ``` 38 3. **`AsNoTracking()`** for read-only queries. 39 4. **Compiled queries** for hot paths. 40 41 ### Configuration 42 43 1. **`IOptions<T>`** pattern for strongly-typed config: 44 ```csharp 45 builder.Services.Configure<DatabaseOptions>(builder.Configuration.GetSection("Database")); 46 ``` 47 48 2. **User secrets** for local development, Key Vault for production. 49 50 ### Middleware 51 52 1. **Custom middleware** for cross-cutting concerns (logging, correlation IDs). 53 2. **`UseExceptionHandler`** for global error handling. 54 55 ### Testing 56 57 1. **xUnit + FluentAssertions:** 58 ```csharp 59 [Fact] 60 public async Task GetTask_ReturnsOk_WhenExists() 61 { 62 var client = _factory.CreateClient(); 63 var response = await client.GetAsync("/api/tasks/1"); 64 response.StatusCode.Should().Be(HttpStatusCode.OK); 65 } 66 ``` 67 68 2. **`WebApplicationFactory<T>`** for integration tests. 69 3. **Respawn** for database cleanup. 70 71 ### Formatting and Static Analysis 72 73 | Tool | Purpose | Command | 74 |---|---|---| 75 | `dotnet format` | Formatting | `dotnet format` | 76 | Roslyn analyzers | Analysis | Built-in | 77 | `dotnet list package --vulnerable` | CVE scanning | Built-in | 78 79 ### Related 80 - C# Idioms @.gemini/skills/csharp-idioms/SKILL.md 81 - API Design Principles @.gemini/skills/api-design-principles/SKILL.md 82 - Database Design Principles @.gemini/skills/database-design-principles/SKILL.md
irahardianto/rugged-gemini/tree/main/.gemini/skills/dotnet-idioms commit 13f8a12b64
Frequently asked questions How do I install the Dotnet Idioms skill? Run npx skillmds@latest add irahardianto/dotnet-idioms in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Dotnet Idioms skill do? .NET Idioms and Patterns It is listed under Coding & Dev Tools on SkillMD.
Is Dotnet Idioms safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Dotnet Idioms? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Dotnet Idioms free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Dotnet Idioms? irahardianto (@irahardianto) published this skill. Their other Agent Skills are listed on their SkillMD profile.