.NET Best Practices

dotnet-best-practices skill for programming & development

Building maintainable, performant .NET applications requires following established patterns and conventions. This skill provides comprehensive guidance on .NET best practices covering architecture patterns, dependency injection, async programming, error handling, security, performance optimization, and testing ensuring high-quality application development.

What Is This?

Overview

.NET Best Practices offers authoritative guidance for .NET development. It recommends proven architecture patterns like clean architecture and DDD, demonstrates proper dependency injection usage, explains async/await best practices, shows effective error handling and logging, covers security considerations and authentication, provides performance optimization techniques, and illustrates comprehensive testing strategies.

The skill synthesizes Microsoft recommendations, community wisdom, and production experience into actionable guidance. It covers .NET 6, 7, 8 and .NET Framework scenarios with framework-specific advice, helping teams navigate version-specific features such as minimal APIs, native AOT compilation, and improved performance primitives introduced across recent releases.

Who Should Use This

.NET developers improving skills. Team leads establishing standards. Architects designing systems. Code reviewers setting guidelines. Junior developers learning patterns. Teams modernizing legacy applications and migrating from older framework versions.

Why Use It?

Problems It Solves

Developers unaware of current best practices write suboptimal code. Guidance ensures modern patterns are followed consistently.

Architecture decisions lack clear rationale. Best practice recommendations provide proven approaches with explained tradeoffs.

Performance issues arise from common mistakes. Guidance prevents typical pitfalls like synchronous blocking, excessive memory allocations, or inefficient database query patterns.

Security vulnerabilities stem from improper patterns. Security best practices prevent common attack vectors.

Core Highlights

Architecture pattern recommendations. Dependency injection guidance. Async/await best practices. Error handling strategies. Security implementation. Performance optimization. Testing approaches. Configuration management. Logging standards.

How to Use It?

Basic Usage

Request guidance on specific .NET topics. The skill provides best practices with rationale and examples.

Show .NET best practices for API development
including error handling and validation
Explain dependency injection best practices
for .NET applications

Specific Scenarios

For architecture, emphasize patterns.

Recommend architecture patterns for
microservices in .NET with example structure

For performance, focus on optimization.

Provide performance best practices for
.NET including memory and async optimization

For security, cover vulnerabilities.

Explain security best practices preventing
common .NET application vulnerabilities

Real World Examples

A team builds new ASP.NET Core API without established standards. Code reviews reveal inconsistent patterns. Best practices guidance provides dependency injection patterns using built-in container, async controller actions with proper cancellation, global exception handling with middleware, structured logging with Serilog, authentication using JWT tokens, validation with FluentValidation, and EF Core patterns with proper transaction handling. Code quality improves significantly with consistent standards applied across the entire codebase.

A developer optimizes slow .NET application. Profiling shows performance issues. Best practice recommendations identify using async/await properly avoiding sync-over-async, reducing allocations with spans and memory pools, caching expensive operations, using compiled queries in EF Core, optimizing LINQ with appropriate methods, and leveraging ValueTask for hot paths. Application performance improves dramatically.

An architect designs new .NET system requiring clear structure. Best practices guidance recommends clean architecture with domain at core, application layer with use cases, infrastructure for external concerns, API layer with controllers, dependency injection configured in composition root, CQRS pattern for complex domains, and event sourcing for audit requirements. Architecture provides clear boundaries and maintainability.

Advanced Tips

Use analyzers enforcing best practices. Configure EditorConfig for consistency. Leverage built-in dependency injection. Prefer async throughout stack. Use spans for high-performance scenarios. Apply SOLID principles. Implement proper logging. Secure secrets properly using environment variables or a secrets manager rather than hardcoded values. Profile before optimizing. Write comprehensive tests.

When to Use It?

Use Cases

Team standard establishment. Code review guidelines. Architecture decisions. Performance optimization. Security hardening. Modernization efforts. Onboarding new developers. Technical debt reduction.

Related Topics

SOLID principles. Clean architecture. Domain-driven design. Dependency injection. Async programming. Performance profiling. Security best practices. Testing strategies.

Important Notes

Requirements

Understanding of C# fundamentals. Familiarity with .NET framework. Knowledge of application type being built. Awareness of performance requirements. Understanding of security concerns.

Usage Recommendations

Follow Microsoft guidelines as baseline. Adapt practices to context. Use analyzers for enforcement. Review practices regularly. Document team-specific standards. Prioritize practices by impact. Balance ideals with pragmatism. Learn rationale not just rules. Update as framework evolves.

Limitations

Best practices evolve over time. Context matters for application. Not all practices apply universally. Trade-offs exist between practices. Requires judgment in application. Cannot replace deep understanding.