What it is
A layered architecture organises an application into horizontal layers, each with one kind of responsibility. The classic four are:
- Presentation: controllers or minimal-API endpoints, request and response models, input checks, HTTP status codes.
- Application: use-case orchestration. Loads what a use case needs, calls the domain, controls the transaction.
- Domain (business logic): the rules of the business. An order needs at least one line; a cancelled order cannot be shipped.
- Data access (persistence, infrastructure): EF Core
DbContext, SQL, repositories, external API clients.
A layer may use the layers below it and never the ones above. In .NET this is often four projects (Shop.Api, Shop.Application, Shop.Domain, Shop.Data) whose project references form a one-way chain.
Why it exists
Without structure, UI code issues SQL, business rules hide in click handlers and nothing can be changed or tested in isolation. Layering is the oldest, simplest answer: separate concerns so each part can be understood on its own, give developers an obvious place for every class, and allow one layer to be replaced (a new UI, a different database) without rewriting the rest. It is easy to explain to a new team, which is a large part of its success.
How it works
A request walks the stack. POST /orders arrives at OrdersController, which maps JSON to a command and calls the application service. The service starts a transaction, asks the domain to create an Order (which enforces its rules) and saves it through the repository. The answer returns up the same path.
public sealed class OrderService(IOrderRepository orders, AppDbContext db)
{
public async Task<Guid> PlaceOrder(PlaceOrderCommand cmd, CancellationToken ct)
{
var order = Order.Create(cmd.CustomerId, cmd.Lines); // domain rules
orders.Add(order);
await db.SaveChangesAsync(ct); // one transaction
return order.Id;
}
}
Strict or relaxed. In strict (closed) layering a layer may only call the one directly below. Every layer is insulated from everything except its neighbour, but simple reads pass through layers that add nothing. In relaxed (open) layering a layer may call any layer below; the presentation layer can run a read-only query without a pass-through service method. Many teams mix the two: commands go through every layer, queries take a short path.
Dependencies point down, toward data. In the traditional form the domain layer references the data layer: it uses the repository classes and often the EF entities themselves. That is what makes changes ripple: a new column changes the entity, then the domain logic that reads it, then the DTO, then the API response.
Layers are not tiers. A layer is a logical grouping of code in one process; calling the next layer is a method call. A tier is a physical deployment unit (a separate process or machine). "Three-tier" (browser, application server, database) says nothing about how the application server's code is layered.
When to use it
- Line-of-business and CRUD-heavy applications with modest business rules.
- Teams that need a structure everyone recognises immediately.
- As a first structure for a small service, with the option to evolve toward clean architecture or vertical slices when it starts to hurt.
When not to
- Rich domains where business rules should not depend on persistence details: use clean or hexagonal architecture so the domain is at the centre.
- Codebases where almost every change is a feature cutting through all layers: vertical slices keep such changes in one place.
- When the "layers" would mostly forward calls: that is ceremony without structure.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Strict layering | Each layer depends on one neighbour only | Pass-through code for simple operations |
| Relaxed layering | Less code, shorter read paths | More dependencies; lower layers harder to change |
| Domain depends on data layer | Simple, direct code; EF entities double as model | Database changes ripple up; domain hard to test without a database |
| Layers as separate tiers | Independent scaling, security zones | Network hops, serialisation, partial failures |
Alternatives
- Clean / onion architecture: same idea of layers, but the domain is in the centre and the data layer depends on interfaces the core owns.
- Hexagonal (ports and adapters): the application core talks to the outside world only through ports; adapters on both sides plug in.
- Vertical slice architecture: organise by feature instead of by technical layer; each slice holds its endpoint, logic and data access.
- Modular monolith: split by business capability first, then choose a structure inside each module.
Failure modes
- Architecture sinkhole: most requests pass through layers that do nothing. A common rule of thumb is to question the structure when this is true of most requests.
- Anemic domain: the "domain layer" holds only property bags; all rules live in services, so the layer exists in name only.
- Leaky layers:
IQueryableor EF entities escape to controllers, so the presentation layer silently depends on the database schema. - Shotgun changes: every feature touches every project; merge conflicts concentrate in the shared service and repository classes.
- Accidental tiers: layers deployed as separate services for no reason, adding latency and failure points.
In production
Enforce the direction with project references, and check it in CI with an architecture test (NetArchTest or ArchUnitNET) so a stray using Shop.Api in the domain fails the build. Keep controllers thin, return DTOs rather than entities, and use AsNoTracking() or projections on the read path. Decide deliberately which reads may bypass layers instead of letting shortcuts accumulate.
Common misconceptions
- "Layers make the app slow." In one process they are method calls. The cost is in change effort, not latency, unless layers are turned into tiers.
- "A repository per table is the data layer." Repositories are one option; EF Core's
DbContextalready is a unit of work and a set of repositories. - "Layered means three-tier." Tiers are deployment, layers are code organisation.
- "Strict layering is always better." It trades simplicity for isolation; for read-heavy code relaxed layering is often the better trade.
Interview angle
Name the layers and the dependency direction, then show you know the weaknesses: changes cut across layers, the domain depends on persistence, strictness leads to pass-through code. Strong answers distinguish layers from tiers, mention enforcement with architecture tests, and explain when you would move to clean architecture (rich rules, testability) or vertical slices (feature-driven change).
Model assumptions
The call order, the strict and relaxed read paths and the change ripple are scripted illustrations: packets are placed by hand and slowed to 30 ms per hop so they can be followed, although in-process calls take microseconds. The runtime steps are simulated: each layer is a server with 16 workers and a 0.5–1 ms mean service time, joined to the next by 0 ms links (in-process calls); the shopper is 20 ms away, the database 1 ms, and a query takes about 6 ms (lognormal, cv 0.3) on a pool of 4 connections. Arrivals are random (Poisson). In the tiers step two links become network hops of 2 ms each way, an illustrative figure for a data-centre hop including serialisation. Numbers show behaviour and orders of magnitude, not benchmarks.