I prefer to do following things when I am setting up new projects :
1. Optimal folder structure
For optimal project organization, I structure my codebase with two distinct folders:
src
for the core application logic and test
for the dedicated unit testing files.
This separation enhances code maintainability and clarity.
2. Utilizing shared class library
For efficient and reusable code in .NET projects, I favor a well-structured approach utilizing shared class library.
These libraries act as central repositories for commonly used functionalities, fostering code maintainability and reducing redundancy across applications such as :
Enums (e.g. Status Codes, Genders etc.)
Constants ( Date Formats, Messages etc.)
Extensions
Models
JSON Convertors/ Factories/Delegating Handlers
3. Organizing the constants
I prefer to create a dedicated Constants.cs
file to store all application-wide constants. This enhances code readability and simplifies maintenance.
In shared class libraries, consider a Constants
folder with feature-specific files for better reusability.
4. Installing necessary libraries
For faster .NET development, consider utilizing essential pre-built libraries at project inception. These are some of my must use :
Serilog for logging
FluentValidation for validations
System.Text.Json for playing with JSON
MediatR to achieve separation of concern with CQRS style
FluentAssertions, NSubstitute and xUnit for unit testing
To learn more about .NET libraries, check this repository.
5. Program.cs should be clean
To promote a well-organized and maintainable codebase in .NET projects, I advocate for a separation of concerns within the Program.cs
file. Here's the approach:
Move dependency registration logic (options, services, middlewares, clients, and validators) to a dedicated file named DependencyInjection.cs
.
Program.cs should only focus on starting the application.
6. Setting up global exception handler
For exceptional user experiences and robust .NET applications, I prioritize implementing a global exception handler at the project setup stage.
This centralized approach effectively manages unexpected errors throughout the application.
In addition to the core practices mentioned earlier, I leverage several tools and strategies to further enhance the .NET development experience:
Tools like GitHub Copilot suggest relevant code for faster development.
Leverage tools like ReSharper to identify issues, suggest optimizations, and enforce coding standards.
Maintain clear and concise documentation (code comments, API documentation) that explains the purpose and usage of your code.
Configure an
editorconfig
file to establish consistent coding styles across your team and project, reducing errors and improving readability.
Thank you for reading, see ya next Saturday!
Whenever you’re ready, there are 2 ways I can help you:
Promote yourself to 9800+ subscribers by sponsoring this newsletter
Patreon Community: Join and gain access to the 200+ articles I have published so far at one place. Join here
Hi Waseem,
Thanks for sharing the insightful content.
Can you please help with GitHub repo or resource from which I can implement a project from scratch which includes .net best practices?
Thanks in advance.
Looking forward to learn by referring your content!