Vertical slice architecture in a .NET 10 API
Most .NET APIs I’ve seen start out organised by layer: Controllers, Services, Repositories, Models.
It looks tidy, but one small change, such as adding a field to an order, touches a file in every one of those
folders. Vertical slice architecture turns this around: you organise the code by feature, so everything one
request needs lives in one place.
This post shows a simple version in a .NET 10 minimal API, with no extra libraries, and when I think it’s worth using.
The idea
A slice is one use case, end to end: the request, the validation, the logic, the data access and the response. Each slice is mostly independent of the others. Instead of asking “which layer does this belong to?”, you ask “which feature does this belong to?”
The folder structure ends up looking like this:
Shop.Api/
├── Features/
│ └── Orders/
│ ├── CreateOrder.cs
│ ├── GetOrder.cs
│ └── CancelOrder.cs
├── Data/
│ ├── AppDbContext.cs
│ └── Order.cs
└── Program.cs
To change how orders are created, you open CreateOrder.cs. That’s usually the whole change.
One slice, one file
Here is a complete slice. It uses a minimal API endpoint, a request and response record, and EF Core directly.
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http.HttpResults;
namespace Shop.Api.Features.Orders;
public static class CreateOrder
{
public record Request(
[Required] string CustomerId,
[Required] string ProductId,
[Range(1, 100)] int Quantity);
public record Response(Guid Id);
public static void Map(IEndpointRouteBuilder app) =>
app.MapPost("/orders", Handle).WithName(nameof(CreateOrder));
private static async Task<Created<Response>> Handle(
Request request, AppDbContext db, CancellationToken ct)
{
var order = new Order
{
Id = Guid.CreateVersion7(),
CustomerId = request.CustomerId,
ProductId = request.ProductId,
Quantity = request.Quantity,
};
db.Orders.Add(order);
await db.SaveChangesAsync(ct);
return TypedResults.Created($"/orders/{order.Id}", new Response(order.Id));
}
}
A few things to notice:
- The request and response types are private to the feature.
CreateOrder.Requestcan change without breakingGetOrder. There’s no sharedOrderDtothat every endpoint depends on. - No service or repository layer. The handler uses the
DbContextdirectly. EF Core is already a unit of work and a repository, so wrapping it again often adds files without adding much. - Typed results. Returning
Created<Response>instead ofIResultmeans the OpenAPI document knows the response shape without extra attributes.
Wiring it up in Program.cs
using Microsoft.EntityFrameworkCore;
using Shop.Api.Features.Orders;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Default")));
builder.Services.AddValidation();
builder.Services.AddOpenApi();
var app = builder.Build();
app.MapOpenApi();
CreateOrder.Map(app);
GetOrder.Map(app);
CancelOrder.Map(app);
app.Run();
AddValidation() is new in .NET 10. It makes minimal APIs check the DataAnnotations on the request
([Required], [Range] and so on) and return a 400 Bad Request with the validation errors before your handler
runs. Before .NET 10 you needed a filter or a library such as FluentValidation for this.
Mapping each slice by hand is fine for a small API. Once there are dozens, a common next step is a small
IEndpoint interface and one extension method that finds and maps every implementation, so Program.cs stops
growing.
Do I need MediatR?
No. A lot of vertical slice examples use MediatR, so people assume the two go together. They don’t. MediatR is useful if you want a pipeline of behaviours (logging, validation, transactions) around every handler, but minimal API endpoint filters and plain middleware cover most of that. Start without it and add it only if you find yourself repeating the same cross-cutting code in many slices.
What about shared code?
Slices will share some things: the DbContext, the entities, authentication, maybe a helper for paging. That’s
expected. The rule I find useful is: duplicate first, extract later. If two slices have similar code, leave it.
When a third one needs it and the code really is the same concept, move it into a shared folder.
Extracting too early is how a “shared” service grows a dozen if statements for the different callers that use it.
Testing
Because each slice is a full request, the most useful tests are usually integration tests that call the endpoint
through WebApplicationFactory and a real database (for example in a Testcontainers PostgreSQL instance). You
test behaviour, not wiring, and refactoring inside a slice doesn’t break the tests.
Unit tests still make sense for pieces of real domain logic, such as a pricing rule. Pull that logic into a plain class and test it directly.
When it’s worth it
Vertical slices work well when:
- the API is mostly many separate use cases, each a fairly thin path from request to database;
- several people work on different features at the same time, since they touch different files and merge conflicts drop;
- you want a new developer to find “the code for X” quickly.
They’re less useful when:
- the API is tiny (three or four endpoints), where any structure works;
- most of the value sits in a rich domain model shared by many features. You’ll still want a proper domain layer, and slices sit on top of it rather than replacing it;
- the team isn’t on board. Mixing layered and sliced styles in one codebase is harder to follow than either one.
Summary
Vertical slice architecture is mainly a decision about where code lives: group it by feature, keep each feature’s types private, and share code only when you have real duplication. In .NET 10, minimal APIs, typed results and built-in validation mean you can do this with just the framework and EF Core.