Architecture in practice
Architecture diagrams are easy. Keeping a backend structured after years of features, several teams and a few rewrites is the hard part. This series collects the patterns I keep coming back to, including the lessons from places where they didn’t work out.
- Stop fetching the same data five times — the Request Context BuilderEvery service in a flow loads the customer "just to be safe", and a single request ends up asking the same question four times. A pattern for gathering data once, validating it once and passing it along, plus the lesson of a codebase that had the pattern and still fetched everything twice.
- Vertical slices — why I cut the code by feature, not by layerA bug report says "placing an order with a voucher fails", and you open five folders to find the code. Why I organize backends around use cases instead of technical layers, what it costs, and how to keep the slices from leaking into each other.
- Folder structure: by technology, by domain, by feature or by feature and action?Four ways to lay out the folders of an ASP.NET Core API, what each one optimizes for and where each one breaks. Plus the structure I recommend today, the namespace trick that keeps it readable, and how registration flows from Program.cs down to a single endpoint.
- Anatomy of a minimal endpointRequest, response, validator, mapper, command, endpoint and documentation. A walk through every part of a single Minimal API endpoint in a feature-sliced backend, what each part is responsible for, and why I split them even though everything would fit into one lambda.
- Mapping as an anti-corruption layerA renamed database column should never break an API client, and a field added by a third-party system should never leak into your domain. Why I treat every mapper as an anti-corruption layer, why I write them by hand (or generate them), and how to test that nothing gets lost.
- CQRS with MediatR, without the ceremonyCQRS doesn't need two databases, event sourcing or a message bus. In most APIs it simply means "a request either reads or writes", and every request gets its own small handler. How I use MediatR for that, the conventions that keep it navigable, the licensing change since v13, and when I'd skip the library entirely.
- MediatR pipeline behaviors: validation and caching without touching a handlerEvery query handler checks its input and every expensive query has the same five lines of cache code. Pipeline behaviors move both out of the handlers into one place. A validation behavior that reports all errors at once, a caching behavior built on HybridCache, and the ordering and key mistakes that will bite you.
- A command that needs several queriesPlacing an order needs the customer, the prices and the stock before anything is written. Should the command handler send queries through the mediator, inject them, or load everything itself? The options, how to run the reads in parallel without breaking your DbContext, and when the "command" is really a query in disguise.
- The cost of indirection: F12 debugging and benchmarking MediatR against direct callsEvery abstraction has a price, paid in navigation, debugging and CPU time. What a mediator really costs you, why F12 suddenly lands on a record, the file convention that fixes most of it, and how to measure the runtime overhead yourself with BenchmarkDotNet instead of trusting anyone's numbers.
- A shared contracts library between API and clientsThe client had an enum with one value fewer than the API, and nobody noticed until deserialization failed. Why copying request and response types into a client project always drifts, how a small contracts package fixes it, what may and may not go into it, and how to stop breaking changes in CI.
- Lightweight ADRs: write down why, not just whatTwo years after a decision, nobody remembers why the team chose it, so the discussion starts again from zero. Architecture decision records fix that with one short Markdown file per decision. The format I use, when a decision deserves one, where to keep them, and a complete example.