mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 14:52:55 +08:00
Regenerate all MDC rules with fresh Exa data and December 2025 best practices
- Refreshed all 241 library rules with current best practices - Updated Exa search results for all libraries with 2025 content - Fixed --regenerate-all flag logic to properly process all libraries - Removed duplicate astro entry in rules.json (merged tags) - Added missing docker.mdc and vim.mdc files - All rules now include modern patterns and anti-patterns - Updated with framework-specific recommendations for 2025 Technical improvements: - Fixed retry_failed_only logic in generate_mdc_files.py - Improved progress tracking for regeneration scenarios - Ensured proper handling of --refresh-exa flag
This commit is contained in:
+393
-366
@@ -1,368 +1,395 @@
|
||||
---
|
||||
description: This rule file provides a comprehensive guide to C# best practices, coding standards, and common patterns for writing maintainable, performant, and secure code.
|
||||
globs: *.cs
|
||||
description: This guide defines definitive C# coding standards and best practices for our team, covering naming, formatting, modern language features, performance, security, and error handling to ensure consistent, maintainable, and high-quality code.
|
||||
globs: **/*
|
||||
---
|
||||
# C# Best Practices and Coding Standards
|
||||
|
||||
This document provides a comprehensive guide to C# best practices, coding standards, and common patterns for writing maintainable, performant, and secure code. It covers various aspects of C# development, including code organization, common patterns, performance considerations, security best practices, testing approaches, common pitfalls, and tooling.
|
||||
|
||||
**Library Information:**
|
||||
- Name: c-sharp
|
||||
- Tags: language, microsoft, dotnet, backend
|
||||
|
||||
## 1. Code Organization and Structure
|
||||
|
||||
A well-organized codebase is crucial for maintainability, scalability, and collaboration. Here are some best practices for organizing your C# code:
|
||||
|
||||
### 1.1. Directory Structure Best Practices
|
||||
|
||||
* **Project Root:** Contains the solution file (.sln) and project directories.
|
||||
* **Project Directory:** Contains the project file (.csproj), source code, and other project-related files.
|
||||
* `src/`: Contains the main source code.
|
||||
* `Models/`: Data models and DTOs (Data Transfer Objects).
|
||||
* `Services/`: Business logic and service classes.
|
||||
* `Controllers/`: API controllers (if applicable).
|
||||
* `Repositories/`: Data access logic.
|
||||
* `Utilities/`: Helper classes and utility functions.
|
||||
* `Exceptions/`: Custom exception definitions.
|
||||
* `Interfaces/`: Interface definitions for abstraction.
|
||||
* `Configuration/`: Configuration-related classes.
|
||||
* `tests/`: Contains unit tests, integration tests, and end-to-end tests.
|
||||
* `docs/`: Documentation for the project.
|
||||
* `build/`: Build scripts and configuration files.
|
||||
* `Properties/`: Assembly information and project settings.
|
||||
|
||||
Example:
|
||||
|
||||
|
||||
MyProject/
|
||||
├── MyProject.sln
|
||||
├── MyProject/
|
||||
│ ├── MyProject.csproj
|
||||
│ ├── src/
|
||||
│ │ ├── Models/
|
||||
│ │ ├── Services/
|
||||
│ │ ├── Controllers/
|
||||
│ │ ├── Repositories/
|
||||
│ │ ├── Utilities/
|
||||
│ │ ├── Exceptions/
|
||||
│ │ ├── Interfaces/
|
||||
│ │ └── Configuration/
|
||||
│ ├── Properties/
|
||||
│ │ └── AssemblyInfo.cs
|
||||
│ └── appsettings.json
|
||||
├── MyProject.Tests/
|
||||
│ ├── MyProject.Tests.csproj
|
||||
│ └── UnitTests/
|
||||
└── README.md
|
||||
|
||||
|
||||
### 1.2. File Naming Conventions
|
||||
|
||||
* **Classes:** PascalCase (e.g., `MyClass.cs`)
|
||||
* **Interfaces:** IPascalCase (e.g., `IMyInterface.cs`)
|
||||
* **Enums:** PascalCase (e.g., `MyEnum.cs`)
|
||||
* **Structs:** PascalCase (e.g., `MyStruct.cs`)
|
||||
* **Delegates:** PascalCase (e.g., `MyDelegate.cs`)
|
||||
* **Configuration Files:** appsettings.json, config.xml
|
||||
* **Test Files:** `MyClassTests.cs`
|
||||
|
||||
### 1.3. Module Organization
|
||||
|
||||
* **Namespaces:** Use namespaces to group related classes and interfaces. Follow a consistent naming convention for namespaces (e.g., `CompanyName.ProjectName.ModuleName`).
|
||||
* **Assemblies:** Divide large projects into multiple assemblies to improve build times, reduce dependencies, and enable code reuse. Consider functional or domain boundaries when creating assemblies.
|
||||
* **NuGet Packages:** Use NuGet packages to manage dependencies and share code across projects.
|
||||
|
||||
### 1.4. Component Architecture
|
||||
|
||||
* **Layered Architecture:** Separate the application into distinct layers (e.g., presentation, business logic, data access) to promote separation of concerns and testability.
|
||||
* **Microservices Architecture:** For large and complex applications, consider a microservices architecture where the application is composed of small, independent services.
|
||||
* **Dependency Injection (DI):** Use DI to manage dependencies between components and improve testability and maintainability. Popular DI containers include Autofac, Ninject, and Microsoft.Extensions.DependencyInjection.
|
||||
|
||||
### 1.5. Code Splitting Strategies
|
||||
|
||||
* **By Feature:** Group code related to a specific feature into a separate module or assembly.
|
||||
* **By Layer:** Separate code based on the architectural layer (e.g., presentation, business logic, data access).
|
||||
* **By Responsibility:** Split classes and methods into smaller, more focused units of work.
|
||||
* **Partial Classes:** Use partial classes to split a large class into multiple files for better organization (use sparingly).
|
||||
|
||||
## 2. Common Patterns and Anti-patterns
|
||||
|
||||
Understanding common design patterns and anti-patterns is essential for writing effective and maintainable C# code.
|
||||
|
||||
### 2.1. Design Patterns
|
||||
|
||||
* **Singleton:** Ensures that a class has only one instance and provides a global point of access to it.
|
||||
* **Factory:** Provides an interface for creating objects without specifying their concrete classes.
|
||||
* **Abstract Factory:** Provides an interface for creating families of related objects without specifying their concrete classes.
|
||||
* **Builder:** Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
|
||||
* **Observer:** Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
|
||||
* **Strategy:** Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
|
||||
* **Dependency Injection (DI):** A technique whereby one object (or static method) supplies the dependencies of another object. This helps to decouple components.
|
||||
* **Repository:** Mediates between the domain and data mapping layers, acting like an in-memory domain object collection.
|
||||
* **Unit of Work:** Maintains a list of objects affected by a business transaction and coordinates the writing out of changes.
|
||||
* **Asynchronous Programming Patterns (TAP, EAP, APM):** Handle asynchronous operations efficiently using async/await.
|
||||
|
||||
### 2.2. Recommended Approaches for Common Tasks
|
||||
|
||||
* **String Manipulation:** Use `StringBuilder` for efficient string concatenation in loops.
|
||||
* **File I/O:** Use `using` statements or `try-finally` blocks to ensure proper disposal of file resources.
|
||||
* **Data Access:** Use ORMs like Entity Framework Core or Dapper for simplified data access.
|
||||
* **Asynchronous Operations:** Use `async` and `await` for non-blocking asynchronous operations.
|
||||
* **Configuration Management:** Use `Microsoft.Extensions.Configuration` for managing application configuration.
|
||||
* **Logging:** Use logging frameworks like Serilog or NLog for structured logging.
|
||||
|
||||
### 2.3. Anti-patterns and Code Smells
|
||||
|
||||
* **God Class:** A class that does too much and has too many responsibilities.
|
||||
* **Long Method:** A method that is too long and complex.
|
||||
* **Feature Envy:** A method that accesses data from another object more than its own.
|
||||
* **Shotgun Surgery:** Changes to one part of the code require changes to many other parts.
|
||||
* **Data Clump:** Groups of data that appear together in multiple places.
|
||||
* **Primitive Obsession:** Using primitive types instead of creating custom classes for domain concepts.
|
||||
* **Switch Statements (over Polymorphism):** Using large switch statements instead of leveraging polymorphism.
|
||||
* **Magic Numbers/Strings:** Hardcoding values directly in the code instead of using constants or configuration settings.
|
||||
* **Ignoring Exceptions:** Catching exceptions without handling them properly.
|
||||
* **Empty Catch Blocks:** Catching exceptions and doing nothing.
|
||||
* **Over-commenting:** Writing excessive comments that are obvious from the code itself.
|
||||
* **Dead Code:** Code that is never executed.
|
||||
|
||||
### 2.4. State Management Best Practices
|
||||
|
||||
* **Stateless Services:** Design services to be stateless whenever possible to improve scalability and reliability.
|
||||
* **Session State:** Use session state sparingly and only when necessary. Consider using distributed caching for session state in web applications.
|
||||
* **Caching:** Use caching to improve performance by storing frequently accessed data in memory.
|
||||
* **Redux/Flux:** For complex UI applications, consider using a state management library like Redux or Flux.
|
||||
* **Immutable Data Structures:** Use immutable data structures to simplify state management and prevent unintended side effects.
|
||||
|
||||
### 2.5. Error Handling Patterns
|
||||
|
||||
* **Try-Catch-Finally:** Use `try-catch-finally` blocks to handle exceptions and ensure proper resource cleanup.
|
||||
* **Exception Filters:** Use exception filters to catch specific exceptions based on certain conditions.
|
||||
* **Custom Exceptions:** Create custom exception types for specific error conditions in your application.
|
||||
* **Logging Exceptions:** Log exceptions with sufficient context to aid in debugging.
|
||||
* **Graceful Degradation:** Handle errors gracefully and provide informative error messages to the user.
|
||||
* **Throw Early, Catch Late:** Detect errors as early as possible and handle exceptions at a higher level.
|
||||
|
||||
## 3. Performance Considerations
|
||||
|
||||
Optimizing C# code for performance is crucial for creating responsive and efficient applications.
|
||||
|
||||
### 3.1. Optimization Techniques
|
||||
|
||||
* **Avoid Boxing and Unboxing:** Boxing and unboxing value types can be expensive. Use generics to avoid boxing and unboxing.
|
||||
* **Use Value Types When Appropriate:** Value types (structs) can be more efficient than reference types (classes) for small, immutable data structures.
|
||||
* **Minimize Object Allocation:** Object allocation can be expensive. Reuse objects whenever possible.
|
||||
* **Use `StringBuilder` for String Concatenation:** `StringBuilder` is more efficient than string concatenation using the `+` operator, especially in loops.
|
||||
* **Optimize LINQ Queries:** Use LINQ carefully and avoid unnecessary iterations or computations. Consider using `AsParallel()` for parallel processing of large collections (with caution, as parallelism introduces complexity).
|
||||
* **Use Asynchronous Programming:** Use `async` and `await` to avoid blocking the main thread and improve responsiveness.
|
||||
* **Avoid Excessive Locking:** Minimize the use of locks to prevent contention and improve concurrency.
|
||||
* **Use Lazy Initialization:** Initialize objects only when they are needed to avoid unnecessary initialization overhead.
|
||||
* **Profile Your Code:** Use profiling tools to identify performance bottlenecks and optimize accordingly. Visual Studio Profiler, dotTrace, and PerfView are good options.
|
||||
|
||||
### 3.2. Memory Management Considerations
|
||||
|
||||
* **Garbage Collection:** Understand how the garbage collector works and avoid creating excessive garbage.
|
||||
* **Dispose of Resources:** Implement the `IDisposable` interface and use `using` statements to ensure proper disposal of resources (e.g., file streams, database connections).
|
||||
* **Weak References:** Use weak references to hold references to objects without preventing them from being garbage collected.
|
||||
* **Object Pooling:** Use object pooling to reuse objects and reduce allocation overhead.
|
||||
* **Large Object Heap (LOH):** Be aware of the Large Object Heap and avoid allocating large objects unnecessarily.
|
||||
|
||||
### 3.3. Rendering Optimization (If Applicable)
|
||||
|
||||
* **UI Virtualization:** Use UI virtualization to render only the visible items in a large list or grid.
|
||||
* **Reduce Overdraw:** Minimize the number of times pixels are drawn on top of each other.
|
||||
* **Batch Rendering:** Batch rendering operations to reduce the number of draw calls.
|
||||
* **Use Hardware Acceleration:** Use hardware acceleration to offload rendering tasks to the GPU.
|
||||
|
||||
### 3.4. Bundle Size Optimization (If Applicable)
|
||||
|
||||
* **Tree Shaking:** Remove unused code from the bundle.
|
||||
* **Code Minification:** Minify code to reduce its size.
|
||||
* **Code Compression:** Compress code using Gzip or Brotli.
|
||||
* **Image Optimization:** Optimize images to reduce their size without sacrificing quality.
|
||||
|
||||
### 3.5. Lazy Loading Strategies
|
||||
|
||||
* **Lazy Initialization:** Use `Lazy<T>` to initialize objects only when they are accessed.
|
||||
* **Virtual Proxy:** Use a virtual proxy to load related data only when it is needed.
|
||||
* **Explicit Loading:** Load related data explicitly using methods like `Include` in Entity Framework Core.
|
||||
|
||||
## 4. Security Best Practices
|
||||
|
||||
Security should be a primary concern in C# development to protect against vulnerabilities and attacks.
|
||||
|
||||
### 4.1. Common Vulnerabilities and Prevention
|
||||
|
||||
* **SQL Injection:** Parameterize database queries to prevent SQL injection attacks.
|
||||
* **Cross-Site Scripting (XSS):** Encode user input to prevent XSS attacks.
|
||||
* **Cross-Site Request Forgery (CSRF):** Use anti-forgery tokens to prevent CSRF attacks.
|
||||
* **Authentication and Authorization Vulnerabilities:** Implement secure authentication and authorization mechanisms.
|
||||
* **Insecure Direct Object References (IDOR):** Validate user access to objects to prevent IDOR attacks.
|
||||
* **File Upload Vulnerabilities:** Validate file types and sizes to prevent malicious file uploads.
|
||||
* **Denial-of-Service (DoS) Attacks:** Implement rate limiting and other measures to prevent DoS attacks.
|
||||
* **Deserialization Vulnerabilities:** Avoid deserializing untrusted data.
|
||||
* **Dependency Vulnerabilities:** Regularly update dependencies to patch security vulnerabilities.
|
||||
|
||||
### 4.2. Input Validation
|
||||
|
||||
* **Validate All User Input:** Validate all user input on both the client and server side.
|
||||
* **Use Regular Expressions:** Use regular expressions to validate input formats.
|
||||
* **Sanitize Input:** Sanitize input to remove or escape potentially malicious characters.
|
||||
* **Use Whitelisting:** Use whitelisting to allow only known good input values.
|
||||
* **Limit Input Length:** Limit the length of input fields to prevent buffer overflows.
|
||||
|
||||
### 4.3. Authentication and Authorization
|
||||
|
||||
* **Use Strong Passwords:** Enforce strong password policies and use password hashing algorithms like bcrypt.
|
||||
* **Implement Multi-Factor Authentication (MFA):** Use MFA to add an extra layer of security to the authentication process.
|
||||
* **Use Role-Based Access Control (RBAC):** Use RBAC to control user access to resources based on their roles.
|
||||
* **Use Claims-Based Authentication:** Use claims-based authentication to represent user identities and permissions.
|
||||
* **Implement Proper Session Management:** Implement secure session management practices, including session timeouts and secure cookies.
|
||||
* **OAuth 2.0 and OpenID Connect:** Leverage industry-standard protocols for authentication and authorization.
|
||||
|
||||
### 4.4. Data Protection
|
||||
|
||||
* **Encrypt Sensitive Data:** Encrypt sensitive data at rest and in transit.
|
||||
* **Use Key Management:** Use a secure key management system to store and manage encryption keys.
|
||||
* **Protect Connection Strings:** Protect connection strings and other sensitive configuration data.
|
||||
* **Implement Auditing:** Implement auditing to track user actions and detect security breaches.
|
||||
|
||||
### 4.5. Secure API Communication
|
||||
|
||||
* **Use HTTPS:** Use HTTPS to encrypt communication between the client and server.
|
||||
* **Implement API Authentication:** Use API keys, JWTs, or other authentication mechanisms to secure API endpoints.
|
||||
* **Rate Limiting:** Implement rate limiting to prevent API abuse.
|
||||
* **Input Validation:** Validate all API input to prevent injection attacks.
|
||||
* **Output Encoding:** Encode API output to prevent XSS attacks.
|
||||
|
||||
## 5. Testing Approaches
|
||||
|
||||
Testing is a critical part of the software development process and helps ensure the quality and reliability of C# applications.
|
||||
|
||||
### 5.1. Unit Testing
|
||||
|
||||
* **Test Individual Units of Code:** Unit tests should focus on testing individual classes, methods, or functions in isolation.
|
||||
* **Use Mocking Frameworks:** Use mocking frameworks like Moq or NSubstitute to isolate units of code and simulate dependencies.
|
||||
* **Follow the Arrange-Act-Assert Pattern:** Arrange the test data, act on the code under test, and assert the expected results.
|
||||
* **Write Clear and Concise Tests:** Write tests that are easy to read and understand.
|
||||
* **Test Edge Cases and Error Conditions:** Test edge cases and error conditions to ensure that the code handles them properly.
|
||||
* **Aim for High Code Coverage:** Aim for high code coverage to ensure that most of the code is tested.
|
||||
|
||||
### 5.2. Integration Testing
|
||||
|
||||
* **Test Interactions Between Components:** Integration tests should focus on testing the interactions between different components or modules of the application.
|
||||
* **Use Real Dependencies or Test Doubles:** Use real dependencies or test doubles to simulate the environment in which the components will operate.
|
||||
* **Test Data Access Logic:** Test data access logic to ensure that it interacts correctly with the database.
|
||||
* **Test API Endpoints:** Test API endpoints to ensure that they handle requests and responses correctly.
|
||||
|
||||
### 5.3. End-to-End Testing
|
||||
|
||||
* **Test the Entire Application Flow:** End-to-end tests should focus on testing the entire application flow from start to finish.
|
||||
* **Use Automation Frameworks:** Use automation frameworks like Selenium or Playwright to automate end-to-end tests.
|
||||
* **Test User Interfaces:** Test user interfaces to ensure that they are functional and user-friendly.
|
||||
* **Test Performance and Scalability:** Test performance and scalability to ensure that the application can handle the expected load.
|
||||
|
||||
### 5.4. Test Organization
|
||||
|
||||
* **Create Separate Test Projects:** Create separate test projects for unit tests, integration tests, and end-to-end tests.
|
||||
* **Organize Tests by Feature or Module:** Organize tests by feature or module to improve maintainability.
|
||||
* **Use Descriptive Test Names:** Use descriptive test names that clearly indicate what the test is verifying.
|
||||
* **Use Test Categories:** Use test categories to group related tests together.
|
||||
|
||||
### 5.5. Mocking and Stubbing
|
||||
|
||||
* **Use Mocking Frameworks:** Use mocking frameworks like Moq or NSubstitute to create mock objects and stub dependencies.
|
||||
* **Create Mock Objects for Dependencies:** Create mock objects for dependencies that are difficult or time-consuming to set up.
|
||||
* **Stub Method Calls:** Stub method calls to return specific values or throw exceptions.
|
||||
* **Verify Method Calls:** Verify that methods are called with the expected arguments.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas
|
||||
|
||||
Being aware of common pitfalls and gotchas can help developers avoid mistakes and write more robust C# code.
|
||||
|
||||
### 6.1. Frequent Mistakes
|
||||
|
||||
* **NullReferenceException:** Not handling null values properly.
|
||||
* **Incorrect Use of Asynchronous Programming:** Blocking the main thread with synchronous operations.
|
||||
* **Memory Leaks:** Not disposing of resources properly.
|
||||
* **Concurrency Issues:** Race conditions, deadlocks, and other concurrency issues.
|
||||
* **Unhandled Exceptions:** Not catching or logging exceptions properly.
|
||||
* **SQL Injection:** Not parameterizing database queries.
|
||||
* **XSS Attacks:** Not encoding user input properly.
|
||||
* **CSRF Attacks:** Not using anti-forgery tokens.
|
||||
* **Ignoring Code Analysis Warnings:** Ignoring warnings from the compiler or code analysis tools.
|
||||
|
||||
### 6.2. Edge Cases
|
||||
|
||||
* **Empty Collections:** Handling empty collections properly.
|
||||
* **Zero Values:** Handling zero values properly.
|
||||
* **Maximum and Minimum Values:** Handling maximum and minimum values properly.
|
||||
* **Date and Time Zones:** Handling date and time zones correctly.
|
||||
* **Unicode Characters:** Handling Unicode characters correctly.
|
||||
|
||||
### 6.3. Version-Specific Issues
|
||||
|
||||
* **C# Language Features:** Being aware of new language features and how they affect existing code.
|
||||
* **.NET Framework vs. .NET Core vs. .NET:** Understanding the differences between the different .NET platforms.
|
||||
* **Breaking Changes:** Being aware of breaking changes in new versions of the .NET framework.
|
||||
|
||||
### 6.4. Compatibility Concerns
|
||||
|
||||
* **Cross-Platform Compatibility:** Ensuring that the code works correctly on different operating systems.
|
||||
* **Backward Compatibility:** Ensuring that the code is compatible with older versions of the .NET framework.
|
||||
* **Interoperability with Other Languages:** Ensuring that the code can interoperate with other languages like C++ or Java.
|
||||
|
||||
### 6.5. Debugging Strategies
|
||||
|
||||
* **Use the Visual Studio Debugger:** Use the Visual Studio debugger to step through code, inspect variables, and set breakpoints.
|
||||
* **Use Logging:** Use logging to track the execution flow of the code and log important information.
|
||||
* **Use Unit Tests:** Use unit tests to isolate and debug individual units of code.
|
||||
* **Use Code Analysis Tools:** Use code analysis tools to identify potential problems in the code.
|
||||
* **Use Profiling Tools:** Use profiling tools to identify performance bottlenecks.
|
||||
|
||||
## 7. Tooling and Environment
|
||||
|
||||
Choosing the right tools and configuring the development environment properly can significantly improve productivity and code quality.
|
||||
|
||||
### 7.1. Recommended Development Tools
|
||||
|
||||
* **Visual Studio:** The primary IDE for C# development.
|
||||
* **Visual Studio Code:** A lightweight code editor with C# support.
|
||||
* **JetBrains Rider:** A cross-platform .NET IDE.
|
||||
* **NuGet Package Manager:** For managing dependencies.
|
||||
* **Git:** For version control.
|
||||
* **Resharper:** Powerful Visual Studio extension for code analysis, refactoring, and navigation.
|
||||
|
||||
### 7.2. Build Configuration
|
||||
|
||||
* **Use MSBuild or dotnet CLI:** Use MSBuild or the dotnet CLI to build the project.
|
||||
* **Configure Build Configurations:** Configure different build configurations for debug and release builds.
|
||||
* **Use NuGet Package Restore:** Use NuGet package restore to automatically download dependencies during the build process.
|
||||
* **Sign Assemblies:** Sign assemblies to prevent tampering.
|
||||
* **Generate Documentation:** Generate documentation from XML comments.
|
||||
* **Enable deterministic builds:** Ensure builds are reproducible by enabling deterministic builds in the project file.
|
||||
|
||||
### 7.3. Linting and Formatting
|
||||
|
||||
* **Use StyleCop Analyzers:** Use StyleCop Analyzers to enforce coding style rules.
|
||||
* **Use Roslyn Analyzers:** Use Roslyn analyzers to detect potential problems in the code.
|
||||
* **Configure EditorConfig:** Use EditorConfig to define coding style rules for the project.
|
||||
* **Use Code Formatters:** Use code formatters like the Visual Studio code formatter to automatically format code.
|
||||
|
||||
### 7.4. Deployment Best Practices
|
||||
|
||||
* **Choose a Deployment Target:** Choose a deployment target based on the application's requirements (e.g., Azure App Service, AWS Elastic Beanstalk, Docker).
|
||||
* **Use a Deployment Pipeline:** Use a deployment pipeline to automate the deployment process.
|
||||
* **Configure Application Settings:** Configure application settings properly for the deployment environment.
|
||||
* **Monitor Application Health:** Monitor application health to detect and resolve issues.
|
||||
|
||||
### 7.5. CI/CD Integration
|
||||
|
||||
* **Use a CI/CD Platform:** Use a CI/CD platform like Azure DevOps, GitHub Actions, or Jenkins to automate the build, test, and deployment process.
|
||||
* **Configure Build Triggers:** Configure build triggers to automatically start builds when code is committed.
|
||||
* **Run Unit Tests and Integration Tests:** Run unit tests and integration tests as part of the build process.
|
||||
* **Deploy to Different Environments:** Deploy the application to different environments (e.g., development, staging, production) as part of the deployment pipeline.
|
||||
* **Automated Code Reviews:** Integrate with code review tools to automate aspects of code review.
|
||||
|
||||
By following these best practices and coding standards, developers can write C# code that is maintainable, performant, secure, and reliable.
|
||||
# c-sharp Best Practices
|
||||
|
||||
This document outlines the definitive C# coding standards and best practices for our team. Adhering to these guidelines ensures consistency, readability, maintainability, and high performance across all our C# projects.
|
||||
|
||||
## Code Organization and Structure
|
||||
|
||||
### 1. Naming Conventions
|
||||
Always follow standard .NET naming conventions to improve readability and predictability.
|
||||
|
||||
* **Types (Classes, Structs, Enums, Delegates, Records):** `PascalCase`
|
||||
* **Interfaces:** `IPascalCase`
|
||||
* **Methods, Public Properties, Events:** `PascalCase`
|
||||
* **Public Constants:** `PascalCase`
|
||||
* **Local Variables, Method Parameters:** `camelCase`
|
||||
* **Private/Protected Fields:** `_camelCase` (underscore prefix)
|
||||
* **Async Methods:** `PascalCase` ending with `Async`
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public class myService {
|
||||
private string name;
|
||||
public void getdata() { /* ... */ }
|
||||
public async Task do_work() { /* ... */ }
|
||||
public interface IMyServiceImplementation { /* ... */ } // Incorrect interface naming
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public class MyService {
|
||||
private readonly ILogger _logger; // Readonly private fields
|
||||
private string _name;
|
||||
public const int MaxRetries = 3; // Public constants are PascalCase
|
||||
|
||||
public MyService(ILogger logger) {
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void GetData() { /* ... */ }
|
||||
public async Task DoWorkAsync() { /* ... */ }
|
||||
}
|
||||
public interface IService { /* ... */ } // Correct interface naming
|
||||
```
|
||||
|
||||
### 2. Formatting
|
||||
Always maintain consistent code layout for clarity.
|
||||
|
||||
* **Indentation:** Use **4 spaces**. Configure your IDE to use spaces, not tabs.
|
||||
* **Braces:** Use [Allman style](https://en.wikipedia.org/wiki/Indent_style#Allman_style) where each brace begins on a new line. Always use braces, even for single-statement blocks.
|
||||
* **`using` Directives:** Place at the top of the file, outside the namespace. Sort alphabetically, with `System` namespaces first.
|
||||
* **Auto-properties:** Keep on a single line.
|
||||
* **Generic Constraints:** Place on separate, indented lines.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
namespace MyNamespace {
|
||||
class MyClass {
|
||||
public int Age {get;set;}
|
||||
void DoSomething() {
|
||||
if(true)
|
||||
Console.WriteLine("Hello");
|
||||
}
|
||||
}}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MyNamespace
|
||||
{
|
||||
public class MyClass
|
||||
{
|
||||
public int Age { get; set; }
|
||||
|
||||
public void DoSomething()
|
||||
{
|
||||
if (true)
|
||||
{
|
||||
Console.WriteLine("Hello");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Modern Language Features
|
||||
Always embrace modern C# features for cleaner, more concise, and safer code.
|
||||
|
||||
* **`var` Keyword:** Use `var` when the type is obvious from the right-hand side of the assignment. Avoid `var` when the type is not immediately clear.
|
||||
* **Expression-bodied Members:** Use for single-expression methods, properties, and accessors.
|
||||
* **Object/Collection Initializers:** Prefer for creating and populating objects/collections.
|
||||
* **Pattern Matching:** Use for type checks and value comparisons.
|
||||
* **Nullable Reference Types:** Enable and address warnings (`#nullable enable`).
|
||||
* **Records:** Use for immutable data transfer objects (DTOs) or value-based types.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
string name = "John Doe";
|
||||
public int GetCount() { return _items.Count; }
|
||||
MyObject obj = new MyObject();
|
||||
obj.Property1 = "Value1";
|
||||
obj.Property2 = 123;
|
||||
if (item is string) { Console.WriteLine((string)item); }
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
var name = "John Doe"; // Type is obvious
|
||||
public int Count => _items.Count; // Expression-bodied property
|
||||
public void Log(string message) => _logger.LogInformation(message); // Expression-bodied method
|
||||
|
||||
var obj = new MyObject {
|
||||
Property1 = "Value1",
|
||||
Property2 = 123
|
||||
};
|
||||
|
||||
if (item is string s) { Console.WriteLine(s); } // Pattern matching
|
||||
```
|
||||
|
||||
## Common Patterns and Anti-patterns
|
||||
|
||||
### 1. Dependency Injection (DI)
|
||||
Always use constructor injection for dependencies. Never use service locators or direct instantiation of dependencies within classes.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public class MyService
|
||||
{
|
||||
private readonly IRepository _repository = new ConcreteRepository(); // Direct instantiation
|
||||
// Or: private readonly IRepository _repository = ServiceLocator.Resolve<IRepository>();
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public class MyService
|
||||
{
|
||||
private readonly IRepository _repository;
|
||||
|
||||
public MyService(IRepository repository) // Constructor injection
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Immutability
|
||||
Favor immutable objects, especially for DTOs and shared state, to reduce side effects and improve thread safety. Use `record` types or `init` accessors.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public class User
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public record User(string FirstName, string LastName); // Immutable record
|
||||
|
||||
// Or for classes:
|
||||
public class Product
|
||||
{
|
||||
public int Id { get; init; } // Init-only setter
|
||||
public string Name { get; init; }
|
||||
}
|
||||
```
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
### 1. Asynchronous Programming
|
||||
Always use `async/await` for I/O-bound operations to prevent blocking threads. Avoid `async void` methods (except for event handlers) and blocking `async` calls.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public void DoWork() {
|
||||
SomeAsyncOperation().Wait(); // Blocks the thread
|
||||
}
|
||||
public async void Event_Click(object sender, EventArgs e) { /* ... */ } // Async void outside event handlers
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public async Task DoWorkAsync() {
|
||||
await SomeAsyncOperation(); // Awaits without blocking
|
||||
}
|
||||
// For event handlers, async void is acceptable
|
||||
public async void Button_Click(object sender, EventArgs e) {
|
||||
await LoadDataAsync();
|
||||
}
|
||||
```
|
||||
|
||||
### 2. String Manipulation
|
||||
Always use `StringBuilder` for concatenating many strings in loops.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
string result = "";
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
result += i.ToString(); // Inefficient in a loop, creates many new strings
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
var sb = new System.Text.StringBuilder();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
sb.Append(i);
|
||||
}
|
||||
string result = sb.ToString();
|
||||
```
|
||||
|
||||
## Common Pitfalls and Gotchas
|
||||
|
||||
### 1. Magic Strings/Numbers
|
||||
Never hardcode string literals or numeric values directly in code. Always use constants or configuration.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public void ProcessOrder(string status) {
|
||||
if (status == "Pending") { /* ... */ }
|
||||
}
|
||||
public decimal CalculateDiscount(decimal price) {
|
||||
return price * 0.10m;
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public static class OrderStatus {
|
||||
public const string Pending = "Pending";
|
||||
public const string Completed = "Completed";
|
||||
}
|
||||
public const decimal DiscountRate = 0.10m;
|
||||
|
||||
public void ProcessOrder(string status) {
|
||||
if (status == OrderStatus.Pending) { /* ... */ }
|
||||
}
|
||||
public decimal CalculateDiscount(decimal price) {
|
||||
return price * DiscountRate;
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Deep Nesting
|
||||
Always refactor deeply nested `if`/`for` blocks to improve readability. Use guard clauses or extract methods.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
if (condition1) {
|
||||
if (condition2) {
|
||||
if (condition3) {
|
||||
// ... complex logic
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
if (!condition1) return;
|
||||
if (!condition2) return;
|
||||
if (!condition3) return;
|
||||
// ... simplified logic
|
||||
```
|
||||
|
||||
## Security Best Practices
|
||||
|
||||
### 1. Input Validation
|
||||
Always validate all external input (user input, API calls, file contents) at the boundaries of your application.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public void CreateUser(string username, string password) {
|
||||
// Directly use inputs without validation
|
||||
_userRepository.Add(new User(username, password));
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public void CreateUser(string username, string password) {
|
||||
if (string.IsNullOrWhiteSpace(username) || username.Length < 5) {
|
||||
throw new ArgumentException("Invalid username.");
|
||||
}
|
||||
// ... more validation (e.g., password strength, SQL injection prevention)
|
||||
_userRepository.Add(new User(username, password));
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Secure Data Handling
|
||||
Never store sensitive information (passwords, API keys) in plain text. Always use secure configuration, environment variables, or secrets management solutions.
|
||||
|
||||
## Error Handling
|
||||
|
||||
### 1. Specific Exceptions
|
||||
Always catch specific exceptions rather than general `Exception`. Log and rethrow when appropriate.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
try {
|
||||
// ...
|
||||
} catch (Exception ex) { // Too broad, hides specific issues
|
||||
_logger.LogError(ex, "An error occurred.");
|
||||
throw; // Rethrows original exception, losing stack trace if not careful
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
try {
|
||||
// ...
|
||||
} catch (FileNotFoundException ex) {
|
||||
_logger.LogError(ex, "File not found at path {Path}.", _filePath);
|
||||
// Specific handling, e.g., notify user
|
||||
} catch (IOException ex) {
|
||||
_logger.LogError(ex, "An I/O error occurred during file operation.");
|
||||
throw; // Rethrow if caller needs to handle, preserving stack trace
|
||||
}
|
||||
```
|
||||
|
||||
### 2. `using` for `IDisposable`
|
||||
Always use the `using` statement for objects that implement `IDisposable` to ensure proper resource cleanup.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
SqlConnection connection = new SqlConnection("...");
|
||||
connection.Open();
|
||||
// ... forgot to close/dispose
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
using (var connection = new SqlConnection("..."))
|
||||
{
|
||||
connection.Open();
|
||||
// ...
|
||||
} // Connection is automatically disposed here
|
||||
```
|
||||
|
||||
## API Design
|
||||
|
||||
### 1. Clear Contracts
|
||||
Always define clear, well-documented API contracts using DTOs and interfaces. Avoid exposing internal implementation details.
|
||||
|
||||
### 2. Asynchronous APIs
|
||||
Always expose `async` methods for any I/O-bound operations in public APIs.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public IEnumerable<Product> GetProducts() { /* Sync I/O operations */ }
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public Task<IEnumerable<Product>> GetProductsAsync() { /* Async I/O operations */ }
|
||||
```
|
||||
|
||||
## Testing Approaches
|
||||
|
||||
### 1. Unit Testing
|
||||
Always write focused unit tests for individual components, mocking external dependencies.
|
||||
|
||||
### 2. Dependency Injection for Testability
|
||||
Always design classes with Dependency Injection to make them easily testable.
|
||||
|
||||
❌ BAD:
|
||||
```csharp
|
||||
public class OrderProcessor {
|
||||
private readonly PaymentGateway _gateway = new PaymentGateway(); // Hard dependency
|
||||
public void Process(Order order) { _gateway.Charge(order.Amount); }
|
||||
}
|
||||
```
|
||||
|
||||
✅ GOOD:
|
||||
```csharp
|
||||
public interface IPaymentGateway { void Charge(decimal amount); }
|
||||
public class PaymentGateway : IPaymentGateway { /* ... */ }
|
||||
|
||||
public class OrderProcessor {
|
||||
private readonly IPaymentGateway _gateway;
|
||||
public OrderProcessor(IPaymentGateway gateway) { _gateway = gateway; }
|
||||
public void Process(Order order) { _gateway.Charge(order.Amount); }
|
||||
}
|
||||
// Now IPaymentGateway can be easily mocked in tests.
|
||||
```
|
||||
Reference in New Issue
Block a user