dotnet-best-practices

Reviews and refactors C# code against SOLID principles and modern .NET standards.

Updated Aug 10, 2025
One-click install
npx skills add https://github.com/afonsoft/ai-studio-workspace --skill dotnet-best-practices-afonsoft
Or copy as Structured Prompt for Agentā–¼
Please help me install this Agent Skill.
Skill: dotnet-best-practices
Source: https://github.com/afonsoft/ai-studio-workspace/tree/main/agents/skills/dotnet-best-practices
Command: npx skills add https://github.com/afonsoft/ai-studio-workspace --skill dotnet-best-practices-afonsoft

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? .NET developers often ship code that violates SOLID principles, misses modern C# features, or contains hidden performance and security issues. This Skill provides structured guidance to review, implement, and refactor C# code so it meets industry standards before reaching production. ## Core Features & Use Cases - Code Review and Validation: Analyzes C# code for SOLID adherence, null safety, error handling, performance, and testability with concrete recommendations. - Best-Practice Implementation: Guides new feature development using dependency injection, pattern matching, records, async/await, and proper naming conventions. - Refactoring Guidance: Identifies code smells and anti-patterns, then rewrites code with explanations covering EF Core optimization, memory management, and security. - Use Case: A developer inherits a legacy OrderService with fat controllers and N+1 EF Core queries. Use this Skill to refactor it into a testable service with constructor injection, AsNoTracking queries, guard clauses, and xUnit tests. ## Quick Start Review my OrderService.cs file for SOLID violations, async mistakes, and EF Core performance issues, then suggest a refactored version.

Frequently Asked Questions about dotnet-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I apply SOLID principles in C# code?ā–¼

Apply SOLID by giving each class one responsibility, depending on abstractions via constructor injection, and creating focused interfaces. The Skill provides concrete examples like an OrderService using IOrderRepository and ILogger injected through the constructor.

How to fix N+1 query problems in Entity Framework Core?ā–¼

Fix N+1 queries by projecting only needed columns with Select, using AsNoTracking for read-only queries, and adding proper indexes on filtered columns. Profiling with Application Insights helps identify the offending queries.

What modern C# features should I use for null safety?ā–¼

Enable nullable reference types with #nullable enable, use ArgumentNullException.ThrowIfNull for guard clauses, and apply null-coalescing operators like ?? and ?. for safe access. These prevent most NullReferenceException cases at compile time.

Does this guidance work with older .NET Framework versions?ā–¼

The Skill targets .NET 6+ and modern C# features like records, init-only properties, and switch expressions. Older .NET Framework projects lack these features, so guidance on pattern matching and nullable reference types would not apply directly.

How do I unit test a service that uses dependency injection?ā–¼

Mock the injected interfaces with Moq, pass the mocks through the constructor, and verify interactions with Times.Once assertions. The Skill demonstrates this pattern with xUnit, testing an OrderService against mocked IOrderRepository and ILogger dependencies.