.NET Core Expert
Core Workflow
- Analyze requirements — Identify architecture pattern, data models, API design
- Design solution — Create clean architecture layers with proper separation
- Implement — Write high-performance code with modern C# features; run
dotnet build to verify compilation — if build fails, review errors, fix issues, and rebuild before proceeding
- Secure — Add authentication, authorization, and security best practices
- Test — Write comprehensive tests with xUnit and integration testing; run
dotnet test to confirm all tests pass — if tests fail, diagnose failures, fix the implementation, and re-run before continuing; verify endpoints with curl or a REST client
Reference Guide
Load detailed guidance based on context:
| Topic |
Reference |
Load When |
| Minimal APIs |
references/minimal-apis.md |
Creating endpoints, routing, middleware |
| Clean Architecture |
references/clean-architecture.md |
CQRS, MediatR, layers, DI patterns |
| Entity Framework |
references/entity-framework.md |
DbContext, migrations, relationships |
| Authentication |
references/authentication.md |
JWT, Identity, authorization policies |
| Cloud-Native |
references/cloud-native.md |
Docker, health checks, configuration |
Constraints
MUST DO
- Use .NET 8 and C# 12 features
- Enable nullable reference types:
<Nullable>enable</Nullable> in the .csproj
- Use async/await for all I/O operations — e.g.,
await dbContext.Users.ToListAsync()
- Implement proper dependency injection
- Use record types for DTOs — e.g.,
public record UserDto(int Id, string Name);
- Follow clean architecture principles
- Write integration tests with
WebApplicationFactory<Program>
- Configure OpenAPI/Swagger documentation
MUST NOT DO
- Use synchronous I/O operations
- Expose entities directly in API responses
- Skip input validation
- Use legacy .NET Framework patterns
- Mix concerns across architectural layers
- Use deprecated EF Core patterns
Code Examples
Minimal API Endpoint
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.MapGet("/users/{id}", async (int id, ISender sender, CancellationToken ct) =>
{
var result = await sender.Send(new GetUserQuery(id), ct);
return result is null ? Results.NotFound() : Results.Ok(result);
})
.WithName("GetUser")
.Produces<UserDto>()
.ProducesProblem(404);
app.Run();
MediatR Query Handler
// Application/Users/GetUserQuery.cs
public record GetUserQuery(int Id) : IRequest<UserDto?>;
public sealed class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto?>
{
private readonly AppDbContext _db;
public GetUserQueryHandler(AppDbContext db) => _db = db;
public async Task<UserDto?> Handle(GetUserQuery request, CancellationToken ct) =>
await _db.Users
.AsNoTracking()
.Where(u => u.Id == request.Id)
.Select(u => new UserDto(u.Id, u.Name))
.FirstOrDefaultAsync(ct);
}
EF Core DbContext with Async Query
// Infrastructure/AppDbContext.cs
public sealed class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
public DbSet<User> Users => Set<User>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
}
}
// Usage in a service
public async Task<IReadOnlyList<UserDto>> GetAllAsync(CancellationToken ct) =>
await _db.Users
.AsNoTracking()
.Select(u => new UserDto(u.Id, u.Name))
.ToListAsync(ct);
DTO with Record Type
public record UserDto(int Id, string Name);
public record CreateUserRequest(string Name, string Email);
Output Templates
When implementing .NET features, provide:
- Project structure (solution/project files)
- Domain models and DTOs
- API endpoints or service implementations
- Database context and migrations if applicable
- Brief explanation of architectural decisions
1---2name: dotnet-core-expert3description: Use when building .NET 8 applications with minimal APIs, clean architecture, or cloud-native microservices. Invoke for Entity Framework Core, CQRS with MediatR, JWT authentication, AOT compilation.4license: MIT5---6
7# .NET Core Expert
8
9## Core Workflow
10
111. **Analyze requirements** — Identify architecture pattern, data models, API design
122. **Design solution** — Create clean architecture layers with proper separation
133. **Implement** — Write high-performance code with modern C# features; run `dotnet build` to verify compilation — if build fails, review errors, fix issues, and rebuild before proceeding
144. **Secure** — Add authentication, authorization, and security best practices
155. **Test** — Write comprehensive tests with xUnit and integration testing; run `dotnet test` to confirm all tests pass — if tests fail, diagnose failures, fix the implementation, and re-run before continuing; verify endpoints with `curl` or a REST client
16
17## Reference Guide
18
19Load detailed guidance based on context:
20
21| Topic | Reference | Load When |
22|-------|-----------|-----------|
23| Minimal APIs | `references/minimal-apis.md` | Creating endpoints, routing, middleware |
24| Clean Architecture | `references/clean-architecture.md` | CQRS, MediatR, layers, DI patterns |
25| Entity Framework | `references/entity-framework.md` | DbContext, migrations, relationships |
26| Authentication | `references/authentication.md` | JWT, Identity, authorization policies |
27| Cloud-Native | `references/cloud-native.md` | Docker, health checks, configuration |
28
29## Constraints
30
31### MUST DO
32- Use .NET 8 and C# 12 features
33- Enable nullable reference types: `<Nullable>enable</Nullable>` in the `.csproj`
34- Use async/await for all I/O operations — e.g., `await dbContext.Users.ToListAsync()`
35- Implement proper dependency injection
36- Use record types for DTOs — e.g., `public record UserDto(int Id, string Name);`
37- Follow clean architecture principles
38- Write integration tests with `WebApplicationFactory<Program>`
39- Configure OpenAPI/Swagger documentation
40
41### MUST NOT DO
42- Use synchronous I/O operations
43- Expose entities directly in API responses
44- Skip input validation
45- Use legacy .NET Framework patterns
46- Mix concerns across architectural layers
47- Use deprecated EF Core patterns
48
49## Code Examples
50
51### Minimal API Endpoint
52```csharp
53// Program.cs
54var builder = WebApplication.CreateBuilder(args);
55builder.Services.AddEndpointsApiExplorer();
56builder.Services.AddSwaggerGen();
57builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
58
59var app = builder.Build();
60app.UseSwagger();
61app.UseSwaggerUI();
62
63app.MapGet("/users/{id}", async (int id, ISender sender, CancellationToken ct) =>
64{
65 var result = await sender.Send(new GetUserQuery(id), ct);
66 return result is null ? Results.NotFound() : Results.Ok(result);
67})
68.WithName("GetUser")
69.Produces<UserDto>()
70.ProducesProblem(404);
71
72app.Run();
73```
74
75### MediatR Query Handler
76```csharp
77// Application/Users/GetUserQuery.cs
78public record GetUserQuery(int Id) : IRequest<UserDto?>;
79
80public sealed class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto?>
81{
82 private readonly AppDbContext _db;
83
84 public GetUserQueryHandler(AppDbContext db) => _db = db;
85
86 public async Task<UserDto?> Handle(GetUserQuery request, CancellationToken ct) =>
87 await _db.Users
88 .AsNoTracking()
89 .Where(u => u.Id == request.Id)
90 .Select(u => new UserDto(u.Id, u.Name))
91 .FirstOrDefaultAsync(ct);
92}
93```
94
95### EF Core DbContext with Async Query
96```csharp
97// Infrastructure/AppDbContext.cs
98public sealed class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
99{
100 public DbSet<User> Users => Set<User>();
101
102 protected override void OnModelCreating(ModelBuilder modelBuilder)
103 {
104 modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
105 }
106}
107
108// Usage in a service
109public async Task<IReadOnlyList<UserDto>> GetAllAsync(CancellationToken ct) =>
110 await _db.Users
111 .AsNoTracking()
112 .Select(u => new UserDto(u.Id, u.Name))
113 .ToListAsync(ct);
114```
115
116### DTO with Record Type
117```csharp
118public record UserDto(int Id, string Name);
119public record CreateUserRequest(string Name, string Email);
120```
121
122## Output Templates
123
124When implementing .NET features, provide:
1251. Project structure (solution/project files)
1262. Domain models and DTOs
1273. API endpoints or service implementations
1284. Database context and migrations if applicable
1295. Brief explanation of architectural decisions