Skip to main content

Lesson 7 of 8

Feature Folders & Vertical Slice Architecture

Vertical Slice Architecture organizes code by feature, not by technical layer. All code for 'Create Product' lives in one folder: command, handler, validator, response DTO, endpoint. This reduces cross-file navigation and makes features independently deployable.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Feature Folder Structure

Feature Folder Structure
// ━━ Vertical Slice structure ━━
// Features/
// ├── Products/
// │   ├── CreateProduct/
// │   │   ├── CreateProductCommand.cs      → record + IRequest<Result<ProductDto>>
// │   │   ├── CreateProductHandler.cs      → IRequestHandler<CreateProductCommand, Result<ProductDto>>
// │   │   ├── CreateProductValidator.cs    → AbstractValidator<CreateProductCommand>
// │   │   └── CreateProductEndpoint.cs     → Minimal API endpoint registration
// │   │
// │   ├── GetProduct/
// │   │   ├── GetProductQuery.cs
// │   │   ├── GetProductHandler.cs
// │   │   └── GetProductEndpoint.cs
// │
// └── Orders/
//     ├── PlaceOrder/
//     │   ├── PlaceOrderCommand.cs
//     │   └── ...

// ━━ Complete Create Product feature slice ━━
// CreateProduct/CreateProductCommand.cs
public record CreateProductSliceCommand(string Name, decimal Price, Guid CategoryId)
    : IRequest<Result<ProductSliceDto>>;

// CreateProduct/CreateProductValidator.cs
public class CreateProductSliceValidator : AbstractValidator<CreateProductSliceCommand>
{
    public CreateProductSliceValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaximumLength(200);
        RuleFor(x => x.Price).GreaterThan(0);
        RuleFor(x => x.CategoryId).NotEmpty();
    }
}

// CreateProduct/CreateProductHandler.cs
public class CreateProductSliceHandler(AppDbContext db)
    : IRequestHandler<CreateProductSliceCommand, Result<ProductSliceDto>>
{
    public async Task<Result<ProductSliceDto>> Handle(
        CreateProductSliceCommand cmd, CancellationToken ct)
    {
        var product = new ProductEntity2 { Id = Guid.NewGuid(), Name = cmd.Name, Price = cmd.Price, CategoryId = cmd.CategoryId, CreatedAt = DateTime.UtcNow };
        db.Products.Add(product);
        await db.SaveChangesAsync(ct);

        return Result<ProductSliceDto>.Success(
            new ProductSliceDto(product.Id, product.Name, product.Price));
    }
}

// CreateProduct/CreateProductEndpoint.cs
public static class CreateProductEndpointExtensions
{
    public static IEndpointRouteBuilder MapCreateProduct(this IEndpointRouteBuilder app)
    {
        app.MapPost("/api/v1/products", async (
            CreateProductSliceCommand command,
            IMediator mediator,
            CancellationToken ct) =>
        {
            var result = await mediator.Send(command, ct);
            return result.IsSuccess
                ? Results.Created($"/api/v1/products/{result.Value!.Id}", result.Value)
                : Results.Conflict(result.Error);
        })
        .WithName("CreateProduct")
        .WithTags("Products")
        .RequireAuthorization("CanManageProducts")
        .WithOpenApi();

        return app;
    }
}

// ━━ Register all endpoints (in Program.cs) ━━
// app.MapCreateProduct();
// app.MapGetProduct();
// app.MapPlaceOrder();

record ProductSliceDto(Guid Id, string Name, decimal Price);
record Result<T>(bool IsSuccess, T? Value = default, string? Error = null) { public static Result<T> Success(T v) => new(true, v); }
abstract class AbstractValidator<T> { protected void RuleFor<TProp>(Func<T, TProp> f) { } }
interface IRequest<T> { }
interface IRequestHandler<TRequest, TResponse> { Task<TResponse> Handle(TRequest r, CancellationToken ct); }
interface IMediator { Task<T> Send<T>(IRequest<T> r, CancellationToken ct); }
class AppDbContext { public System.Collections.Generic.List<ProductEntity2> Products { get; set; } = []; public Task<int> SaveChangesAsync(CancellationToken ct) => Task.FromResult(0); }
class ProductEntity2 { public Guid Id { get; set; } public string Name { get; set; } = ""; public decimal Price { get; set; } public Guid CategoryId { get; set; } public DateTime CreatedAt { get; set; } }

Tip

Tip

Practice Feature Folders Vertical Slice Architecture in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of Feature Folders Vertical Slice Architecture from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Feature Folders Vertical Slice Architecture is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/8 done