Dotnet Guru
Enforce modern, production-grade C# and .NET practices on every task. Default to the latest stable SDK available in the repository, and prefer current LTS for new projects. Never suggest legacy patterns when a modern built-in .NET approach exists.
Critical
- Treat warnings as quality signals; do not ignore analyzer warnings without reason.
- Prefer BCL/SDK-native solutions before third-party dependencies.
- Keep implementations trim-safe and AOT-aware where practical.
- Preserve readability: simple, explicit, measurable improvements over cleverness.
- For new project/setup guidance, always include concrete quality commands:
dotnet format,dotnet test, and build with warnings-as-errors. - For DTO/API design guidance, always state nullability explicitly (which properties are nullable vs non-nullable and why).
- Treat "singleton depends on scoped" as an invalid lifetime configuration (captive dependency) and say so clearly.
Response Guardrails
- When asked to set up a .NET project, include a short "Quality Gate" block with:
dotnet build -warnaserrordotnet format --verify-no-changesdotnet test - When asked about models/DTOs/contracts, include one explicit nullability note (for example,
stringvsstring?). - When asked about DI lifetimes, explicitly validate lifetime compatibility and call out captive dependencies.
When to Use
- Writing new C#/.NET code (libraries, services, APIs, workers, tooling)
- Reviewing or refactoring C#/.NET code for correctness or maintainability
- Designing ASP.NET Core endpoints and middleware
- Optimising EF Core data access and query performance
- Setting up .NET project tooling, testing, and CI
- Debugging async, DI lifetimes, configuration, or performance issues
When NOT to Use
- Non-.NET language tasks where C# is incidental
- High-level architecture with no concrete .NET implementation
- Ops-only tasks unrelated to .NET code authoring
Enforcement Checklist
Run this checklist for every C#/.NET answer.
SDK & Tooling
- ☐ Target latest stable SDK in repo (prefer current LTS for greenfield)
- ☐
Nullableenabled and warnings treated as errors where feasible - ☐
ImplicitUsingsenabled (unless project has explicit reason not to) - ☐ Roslyn analyzers + code analysis enabled (
EnableNETAnalyzers=true) - ☐
.editorconfigand analyzer severity aligned with team rules - ☐
dotnet formatanddotnet testused in CI
Language & API Design
- ☐ Use file-scoped namespaces and modern concise syntax when it improves clarity
- ☐ Prefer
required/init/records for immutable or DTO-style models - ☐ Prefer pattern matching and switch expressions over nested conditionals
- ☐ Use collection expressions where supported and readable
- ☐ Public APIs use cancellation tokens for async I/O paths
- ☐ Avoid
dynamicunless interop truly requires it
Async & Performance
- ☐ No sync-over-async (
.Result,.Wait(), blocking I/O in async flows) - ☐ No
Task.Runinside ASP.NET Core request path for I/O work - ☐ Use async end-to-end for I/O (
ToListAsync,ReadAsync,SendAsync) - ☐ Consider hot-path allocations (
ArrayPool<T>,Span<T>, projection) - ☐ Return paged results for large collections
- ☐ Avoid exception-driven control flow on hot paths
ASP.NET Core
- ☐ Prefer minimal APIs/controllers with explicit validation and result contracts
- ☐ Register middleware in correct order (authn/authz/error handling etc.)
- ☐ Avoid capturing
HttpContextbeyond request lifetime - ☐ Use
IHttpClientFactoryfor outbound HTTP - ☐ Use health checks, structured logging, and OpenTelemetry-ready instrumentation
DI & Configuration
- ☐ Service lifetimes are correct (no scoped-in-singleton captive dependencies)
- ☐ Avoid service locator anti-pattern (
GetServicein app logic) - ☐ Use Options pattern for config sections (typed options)
- ☐ Validate options (
ValidateDataAnnotations/ValidateOnStartas needed) - ☐ Avoid
BuildServiceProviderduring registrations
Data Access (EF Core)
- ☐ Query only required columns (
Select) and limit result size (Take/pagination) - ☐ Use
AsNoTrackingfor read-only queries - ☐ Avoid N+1; prefer explicit eager loading and filtered includes
- ☐ Prefer async EF APIs consistently
- ☐ Use indexes/migrations intentionally and review query plans for hot queries
Serialisation & Security
- ☐ Prefer
System.Text.Jsonas default serialiser - ☐ Use source-generated JSON context for trim/AOT/perf-sensitive workloads
- ☐ Validate and bound request payload sizes
- ☐ No secrets in code; use config providers/secret stores
- ☐ Parameterise SQL and avoid unsafe deserialisation patterns
Testing
- ☐ Unit tests are deterministic and isolated (
xUnit+FluentAssertionsoptional) - ☐ Integration tests use realistic hosts (
WebApplicationFactory, Testcontainers) - ☐ Use
Theoryfor data-driven tests instead of logic-heavy test bodies - ☐ Include cancellation/error-path tests for async methods
- ☐ Add performance-focused benchmarks for critical paths (
BenchmarkDotNet)
Anti-Patterns (instant fail)
- ☐ No
async void(except event handlers) - ☐ No
.Result/.Wait()in application code - ☐ No fire-and-forget tasks without explicit supervision strategy
- ☐ No singleton services that capture scoped dependencies
- ☐ No EF lazy-loading reliance in high-throughput request paths
- ☐ No broad
catch (Exception)without meaningful handling and rethrow policy
Quick Reference: Old → Modern
| Avoid | Prefer |
|---|---|
.Result / .Wait() | await all the way |
new HttpClient() per call | IHttpClientFactory |
| Stringly-typed config reads | Options pattern + validation |
Newtonsoft.Json by default | System.Text.Json (source-gen where useful) |
| Loading full entities always | Projection with Select(...) |
| Tracked reads by default | AsNoTracking() for read-only |
| Service locator access | Constructor injection |
| Manually created scopes everywhere | Proper scoped lifetimes + hosted services |
New Project Bootstrap
Use this baseline for a modern API/service:
my-dotnet-app/
├── src/
│ └── MyApp/
│ ├── Program.cs
│ ├── appsettings.json
│ └── Features/
├── tests/
│ ├── MyApp.UnitTests/
│ └── MyApp.IntegrationTests/
├── Directory.Build.props
├── .editorconfig
└── global.json
Recommended setup commands:
dotnet new webapi -n MyApp
cd MyApp
dotnet new xunit -n MyApp.UnitTests -o ../tests/MyApp.UnitTests
dotnet new xunit -n MyApp.IntegrationTests -o ../tests/MyApp.IntegrationTests
dotnet add ../tests/MyApp.UnitTests package FluentAssertions
dotnet add ../tests/MyApp.IntegrationTests package Microsoft.AspNetCore.Mvc.Testing
dotnet add ../tests/MyApp.IntegrationTests package Testcontainers
Validation Guidelines
Before final output:
- Confirm code targets project SDK constraints.
- Re-check nullability, lifetimes, and cancellation behaviour.
- Re-check query shape, serialisation defaults, and secret handling.
- Provide the simplest safe implementation that passes analyzers/tests.
- If tradeoffs exist, state them explicitly with measurable impact.
References
Load these files for depth as needed:
references/tooling.md: SDK pinning, analyzers, formatting, CIreferences/language-design.md: modern C# language and API design patternsreferences/async-performance.md: async correctness and performance patternsreferences/webapi.md: ASP.NET Core endpoint, middleware, observabilityreferences/data-access.md: EF Core querying, performance, migrationsreferences/testing.md: unit/integration testing and benchmark practicesreferences/security.md: secure coding and hardening for .NET servicesreferences/anti-patterns.md: common .NET anti-patterns and remediationsreferences/known-traps.md: failure modes whose symptom points the wrong way