DEV Community

Janki Mehta
Janki Mehta

Posted on

How to Write Clean Code with ASP.NET Core

Writing clean code is extremely important for developing maintainable and readable applications. ASP.NET Core provides developers with tools and features that make it easier to implement principles of clean coding in web applications. In this blog post, we will look at different aspects of writing clean ASP.NET Core code by following clean coding best practices.

Naming Conventions

Naming is one of the most important aspects of clean code. Good naming helps convey the intent and purpose of code at a glance. ASP.NET Core recommends following C# naming conventions for classes, methods, variables etc. Some key points:

  • Use descriptive yet concise names for classes, methods and variables. Avoid abbreviations.
  • Name classes and namespaces based on functionality rather than technical implementation.
  • Use PascalCasing for all identifiers except parameter names which use camelCasing.
  • Keep parameter names short and meaningful.

For example:

public class ProductManager {
  public Task<Product> GetProductByIdAsync(int id) {
Enter fullscreen mode Exit fullscreen mode

Consistent and clear naming makes code more readable and maintainable.

Separation of Concerns

Separating code based on task or layer is critical for clean code. ASP.NET Core encourages separation of concerns through:

  • Separate page models, services and data access logic into different classes.
  • Isolate UI, logic and data access code using MVC patterns. View layer calls controller which calls services.
  • Create independent class libraries for cross-cutting concerns like logging, caching etc.
  • Modularize code into scoped packages and NuGet packages.

For example separating Product controller and service:

public class ProductsController: Controller {
  private IProductService _productService;

  public ProductsController(IProductService productService) {
    _productService = productService;

  public IActionResult Index() => View(_productService.GetAll());

public class ProductService : IProductService{
  public IEnumerable<Product> GetAll() {
Enter fullscreen mode Exit fullscreen mode

This improves reusability, testability and maintainability.

Single Responsibility Principle

ASP.NET Core encourages applying single responsibility principle by designing focused classes with a single well-defined purpose.

  • Classes should have only one job or responsibility to avoid tight coupling.
  • Divide functionality into classes based on rule of thumb - "One class, one job".
  • Split classes into smaller, more manageable units of code.

For example, separating product data access:

public class ProductDatabase {
  // data access only

public class ProductRepository : IProductRepository{
  private ProductDatabase _db;

  public ProductRepository(ProductDatabase db) {
    _db = db; 

  public IEnumerable<Product> GetAll() {
    return _db.GetProducts(); 
Enter fullscreen mode Exit fullscreen mode

This makes code more modular and allows easier extension and maintenance.

Dependency Inversion Principle

Depend upon abstractions, not concretions is another important principle supported by ASP.NET Core's built-in DI framework.

  • Define interfaces for dependent components and consume these interfaces rather than concrete classes.
  • Depend upon abstractions defined in higher more general layers.
  • Make high level or application modules independent of lower level modules.

For example:

public interface IProductRepository {
  IEnumerable<Product> GetAll();

public class ProductRepository : IProductRepository {
  // implementation

public class ProductsController : Controller {

  private readonly IProductRepository _repo;

  public ProductsController(IProductRepository repo) {
    _repo = repo;

Enter fullscreen mode Exit fullscreen mode

This makes code loosely coupled, extensible and testable. DI resolves concrete types during runtime.


Writing clean ASP.NET Core code means following patterns that make code highly testable.

  • Favor dependency injection over direct instantiation for testable seams.
  • Take dependencies through constructor injection rather than property injection.
  • Avoid direct references to static/global objects and ASP.NET Core specific services.
  • Isolate data access and external communications behind interfaces.
  • Keep view logic minimal to avoid UI testing complex logic.

For example:

public class ProductServiceTest {

  private Mock<IProductRepository> _mockRepo;

  public ProductServiceTest() {
    _mockRepo = new Mock<IProductRepository>(); 

  public async Task GetProductsTest() {
   // arrange

   // act

   // assert

Enter fullscreen mode Exit fullscreen mode

This makes code testable without depending on runtime environments.


Finally, readable code is clean code. Some tips for readability in ASP.NET Core projects:

  • Format code consistently using formatting tools.
  • Add comments for complex code explaining intent, not what it does.
  • Limit line lengths to maintainability.
  • Extract code sections to well named functions/methods with single abstraction.
  • Avoid deep nesting and long methods - split into sub-functions.
  • Use whitespace effectively to group related variables, operators etc.

Following these best practices of clean coding makes ASP.NET Core code maintainable, extensible and production-ready. Let me know if any part needs more explanation!

Final Thought

In this journey, I delved deep into the principles of clean code, emphasizing naming conventions, separation of concerns, single responsibility, and dependency inversion—core tenets strongly advocated by ASP.NET Core. Additionally, we explored strategies to elevate code to the pinnacle of testability and readability. By adhering to these fundamentals, you'll find yourself in possession of code that not only brings joy to work with but also ensures ease of maintenance over the long term.

And remember, to truly excel in ASP.NET Core development, don't hesitate to hire skilled ASP.NET Core developers who are adept at applying these principles effectively.

Top comments (0)