$npx -y skills add tranhieutt/software_development_department --skill dotnet-backend-patternsProvides .NET and ASP.NET Core patterns for REST APIs, Entity Framework, dependency injection, and middleware. Use when working with C# files (*.cs, *.csproj) or when the user mentions .NET, ASP.NET Core, C#, or Entity Framework.
| 1 | # .NET Backend Development Patterns |
| 2 | |
| 3 | C#/.NET patterns for production-grade APIs, MCP servers, and enterprise backends. |
| 4 | |
| 5 | ## API Structure (Minimal API + Controllers) |
| 6 | |
| 7 | \`\`\`csharp |
| 8 | // Program.cs - Minimal API |
| 9 | var builder = WebApplication.CreateBuilder(args); |
| 10 | builder.Services.AddDbContext<AppDbContext>(o => o.UseNpgsql(connStr)); |
| 11 | builder.Services.AddScoped<IOrderService, OrderService>(); |
| 12 | |
| 13 | var app = builder.Build(); |
| 14 | app.MapGet("/orders/{id}", async (int id, IOrderService svc) => |
| 15 | await svc.GetByIdAsync(id) is { } order ? Results.Ok(order) : Results.NotFound()); |
| 16 | \`\`\` |
| 17 | |
| 18 | ## Dependency Injection Patterns |
| 19 | |
| 20 | \`\`\`csharp |
| 21 | // Register services |
| 22 | builder.Services.AddScoped<IPaymentService, StripePaymentService>(); |
| 23 | builder.Services.AddSingleton<ICacheService, RedisCacheService>(); |
| 24 | builder.Services.AddHttpClient<IApiClient, ExternalApiClient>(client => { |
| 25 | client.BaseAddress = new Uri("https://api.external.com"); |
| 26 | }); |
| 27 | \`\`\` |
| 28 | |
| 29 | Lifetime guide: Singleton (stateless/cache), Scoped (per-request), Transient (stateless utility). |
| 30 | |
| 31 | ## Entity Framework Core |
| 32 | |
| 33 | \`\`\`csharp |
| 34 | // DbContext with conventions |
| 35 | public class AppDbContext : DbContext { |
| 36 | public DbSet<Order> Orders => Set<Order>(); |
| 37 | protected override void OnModelCreating(ModelBuilder builder) { |
| 38 | builder.Entity<Order>().HasIndex(o => o.UserId); |
| 39 | builder.Entity<Order>().Property(o => o.Total).HasPrecision(18, 2); |
| 40 | } |
| 41 | } |
| 42 | \`\`\` |
| 43 | |
| 44 | Performance tips: |
| 45 | - Use `.AsNoTracking()` for read-only queries |
| 46 | - Avoid N+1 with `.Include()` or projection |
| 47 | - Use compiled queries for hot paths |
| 48 | |
| 49 | ## Middleware Pipeline |
| 50 | |
| 51 | \`\`\`csharp |
| 52 | app.UseMiddleware<RequestLoggingMiddleware>(); |
| 53 | app.UseAuthentication(); |
| 54 | app.UseAuthorization(); |
| 55 | app.UseRateLimiter(); |
| 56 | app.MapControllers(); |
| 57 | \`\`\` |
| 58 | |
| 59 | ## Error Handling |
| 60 | |
| 61 | \`\`\`csharp |
| 62 | // Global exception handler |
| 63 | app.UseExceptionHandler(err => err.Run(async context => { |
| 64 | var exception = context.Features.Get<IExceptionHandlerFeature>()?.Error; |
| 65 | var (status, message) = exception switch { |
| 66 | NotFoundException => (404, exception.Message), |
| 67 | UnauthorizedAccessException => (403, "Forbidden"), |
| 68 | _ => (500, "Internal server error") |
| 69 | }; |
| 70 | context.Response.StatusCode = status; |
| 71 | await context.Response.WriteAsJsonAsync(new { error = message }); |
| 72 | })); |
| 73 | \`\`\` |
| 74 | |
| 75 | ## Configuration (IOptions pattern) |
| 76 | |
| 77 | \`\`\`csharp |
| 78 | builder.Services.Configure<StripeSettings>( |
| 79 | builder.Configuration.GetSection("Stripe")); |
| 80 | |
| 81 | // Usage |
| 82 | public class PaymentService(IOptions<StripeSettings> opts) { |
| 83 | private readonly string _key = opts.Value.SecretKey; |
| 84 | } |
| 85 | \`\`\` |
| 86 | |
| 87 | ## Testing |
| 88 | |
| 89 | \`\`\`csharp |
| 90 | // Integration test with WebApplicationFactory |
| 91 | public class OrderApiTests : IClassFixture<WebApplicationFactory<Program>> { |
| 92 | private readonly HttpClient _client; |
| 93 | [Fact] |
| 94 | public async Task GetOrder_ReturnsOk() { |
| 95 | var response = await _client.GetAsync("/orders/1"); |
| 96 | response.StatusCode.Should().Be(HttpStatusCode.OK); |
| 97 | } |
| 98 | } |
| 99 | \`\`\` |
| 100 | |
| 101 | ## Related Skills |
| 102 | |
| 103 | - `backend-architect` — architecture decisions |
| 104 | - `database-architect` — schema design |
| 105 | - `drizzle-orm-expert` — Node.js ORM alternative |