Clean Architecture in ASP.NET Core without the ceremony

.NET2 min read

Network cables connected to a server rack
Photo: Taylor VickUnsplash

Clean Architecture has a reputation for ceremony: a dozen projects, an interface for every class, and three mapping layers between the database and the browser. It does not have to look like that. The useful part is one rule, and you can apply it in a small ASP.NET Core solution without the rest.

The one rule

Source code dependencies point inward. The domain knows nothing about the application layer, the application layer knows nothing about EF Core or HTTP, and the outer layers plug in through interfaces the inner layers define.

In practice I use four projects:

  • Domain: entities, value objects and the rules that must always hold. No NuGet packages beyond the base library.
  • Application: use cases. Each one is a command or a query with a handler and, where needed, a validator. This layer defines the ports it needs, such as IInquiryRepository or IEmailSender.
  • Infrastructure: EF Core, SMTP, file storage and HTTP clients. It implements the ports.
  • Api: minimal API endpoints that translate HTTP into commands and queries and results back into HTTP.

Use cases as small handlers

A handler does one thing:

public sealed record SubmitInquiryCommand(string Name, string Email, string Message) : ICommand<Guid>;

internal sealed class SubmitInquiryHandler(IInquiryRepository inquiries, IClock clock)
    : ICommandHandler<SubmitInquiryCommand, Guid>
{
    public async Task<Result<Guid>> HandleAsync(SubmitInquiryCommand command, CancellationToken ct)
    {
        var inquiry = Inquiry.Submit(command.Name, command.Email, command.Message, clock.UtcNow);
        await inquiries.AddAsync(inquiry, ct);
        return inquiry.Id;
    }
}

The endpoint is two lines: build the command, dispatch it, map the result. Validation runs in a pipeline step before the handler, so the handler only sees valid input.

Where I skip the ceremony

  • No repository for every table. If a query only reads data for one screen, a read-model query in Infrastructure is fine. Repositories are for aggregates that have rules.
  • No AutoMapper. A static ToDto method is easier to read, easier to debug and fails at compile time.
  • No generic IRepository<T>. It leaks query details into the application layer and hides what each use case actually needs.
  • One Result type instead of exceptions for expected failures. "Not found" and "validation failed" are normal outcomes, not crashes.

What you get back

The payoff shows up in tests and in change:

  • Domain and application tests run in milliseconds with fake ports and no database.
  • Integration tests use WebApplicationFactory with SQLite, so they exercise the real endpoints and EF mappings.
  • Swapping SMTP for another email provider, or SQL Server for SQLite in tests, touches one class in Infrastructure.

Keep the dependency rule, keep handlers small, and drop the parts that only add files.

Have a platform in mind?

Tell me who will use it and what they need to do. I'll reply with questions and next steps.

Join the conversation

No comments yet. Be the first to share your thoughts.

Leave a comment

Questions, ideas or a different point of view are all welcome. Your comment will appear here once it has been approved.

Your email is never shown publicly. It is only used if you ask to be told about a reply.