Implementing Clean Architecture with .NET 5

Clean Architecture (Robert C. Martin) separates concerns into concentric rings, with the Domain at the center. Dependencies point inward. We implement this in .NET 5.

The Layers

flowchart TB
    subgraph Core ["Core (No Dependencies)"]
        Domain[Domain Entities]
        App[Application Use Cases]
    end
    
    subgraph Outer ["Infrastructure"]
        Infra[EF Core / SQL]
        API[Web API Controllers]
    end
    
    App --> Domain
    Infra --> App
    API --> App
    
    style Core fill:#FFF3E0,stroke:#E65100

The Dependency Rule

The Application layer defines interfaces (e.g., `IOrderRepository`), but the Infrastructure layer implements them. This “Inversion of Control” keeps the core independent of the database.

// Core/Interfaces/IOrderRepo.cs
public interface IOrderRepository
{
    Task SaveAsync(Order order);
}

// Infrastructure/Persistence/OrderRepo.cs
public class OrderRepository : IOrderRepository
{
    private readonly DbContext _context;
    public async Task SaveAsync(Order order) => await _context.AddAsync(order);
}

Key Takeaways

  • Domain entities should have no external dependencies (no attribute tags if possible).
  • Use CQRS (MediatR) in the Application layer to handle use cases.
  • Infrastructure maps Domain entities to Database tables.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT SQL_CALC_FOUND_ROWS cmxg4_comments.comment_ID FROM cmxg4_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 15485 AND comment_type NOT IN ('note') ORDER BY cmxg4_comments.comment_date_gmt ASC, cmxg4_comments.comment_ID ASC

Leave a comment

Your email address will not be published. Required fields are marked *

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'akismet_comment_nonce' LIMIT 1

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'cookie_consent_template' LIMIT 1

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'jpsq_sync_checkout'

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT COUNT(*) FROM cmxg4_jetpack_sync_queue WHERE queue_id = 'sync'