Sub topic 10
Layering inside and across features
This chapter assumes you know feature folders, barrels, and route-scoped providers from the earlier chapters. It adds a second axis of structure: what kind of code each piece is, and which kinds may depend on which.
Four layer types
A feature folder can hold code with very different jobs. Naming those jobs keeps the folder from turning into a pile.
| Layer | Holds | May depend on |
|---|---|---|
| feature | Routed, smart components that wire things together | ui, data-access, util |
| ui | Presentational components that take inputs and emit outputs | ui, util |
| data-access | Stores, HTTP services, and models | data-access, util |
| util | Pure functions, types, and helpers | util |
The direction matters more than the names. Dependencies point downward only. A ui component never injects a store, and a util function never imports a component.
Layers inside one feature
Inside a single feature, layers can be subfolders:
orders/
feature/
order-list.component.ts
order-detail.component.ts
ui/
order-row.component.ts
data-access/
orders.store.ts
order.model.ts
util/
format-order-total.ts
orders.routes.ts
index.tsThe feature folder contains the components the router loads. These components inject OrdersStore and pass plain data down to ui components.
import { Component, inject } from '@angular/core';
import { OrdersStore } from '../data-access/orders.store';
import { OrderRowComponent } from '../ui/order-row.component';
@Component({
selector: 'app-order-list',
standalone: true,
imports: [OrderRowComponent],
template: `
@for (order of store.orders(); track order.id) {
<app-order-row [order]="order" />
}
`,
})
export class OrderListComponent {
protected readonly store = inject(OrdersStore);
}OrderRowComponent receives order as an input and knows nothing about the store. You can reuse it, test it with a plain input, and move it to shared later without changing it.
Layers across features
The same names apply at the app level. Each feature is a vertical slice, and some slices serve everyone:
shared/uiholds generic components such as buttons and dialogs.shared/utilholds helpers such as date formatting.coreholds app-wide singletons such as the auth service and HTTP interceptors.
A feature may import from shared and core. shared must not import any feature. If shared/ui imports orders, a change in orders can break every other feature, and you lose the isolation you were building.
Common errors
- Smart ui components. A "ui" component that injects a store is really a feature component. Move the injection up and pass data through inputs.
- Util that knows the domain. A helper that imports
OrdersStoreis data-access. Util code should depend on nothing but types and other util code. - Feature-to-feature imports of internals. Reaching into
orders/data-access/...fromcustomersbypasses the barrel. Import only through@app/orders. - Shared as a dumping ground. Code used by one feature stays in that feature. Move it to
sharedwhen a second feature needs it.
Choosing a layer
When you are unsure where a file belongs, ask these questions in order:
- Does it touch the router, or does it coordinate several pieces? It is feature.
- Does it fetch, hold, or persist state? It is data-access.
- Does it only render inputs and emit events? It is ui.
- Is it a pure function or a type with no Angular dependencies? It is util.
Exercise
A component named OrderSummaryCardComponent injects OrdersStore to read the current order and renders a card. Another feature wants to reuse the card. Which layer does it belong to now, and how would you restructure it so it can be reused?
Show answer
Because it injects a store, it is a feature-layer component, and other features should not import it. To make it reusable, split it. Create a ui component that takes order as an input and renders the card, and keep a thin feature component that injects OrdersStore and passes the order down. The ui component can then move to shared/ui if more than one feature needs it.