A small, lint-enforced layering convention for Go applications.
DCBA splits an application's internal/ tree into four sibling layers and fixes
the direction dependencies may point. Same idea as Hexagonal / Clean / Onion
architecture, boiled down to four directories and one rule you can enforce with a
linter.
This repo is the canonical spec: reference it from a project's
internal/doc.go instead of copying the whole explanation into every codebase.
| Layer | What lives here | |
|---|---|---|
| D | dtos/ |
wire shapes - request/response DTOs; maps domain ⇄ JSON (or protobuf, etc.) |
| C | components/ |
infrastructure - db, auth, external API clients, caches, storage |
| B | business/ |
the domain - entities and domain rules. The dependency sink. |
| A | application/ |
composition root + transport (handlers/routing) + use cases (services) |
The letters are deliberately not in top-to-bottom order. Alphabetical "ABCD" reads like a stack where A is the foundation - but here A is the outermost layer, not the base. Naming them D-C-B-A stops that wrong mental picture. The label is just a mnemonic for the four directories; the real contract is the dependency rule.
Dependencies point inward, toward the domain. The only allowed directions:
A ──▶ C, D, B application may import anything
C ──▶ B components may import the domain only
D ──▶ B dtos may import the domain only
B ──▶ (nothing) business imports no sibling layer*
* business/* packages may import other business/* packages (e.g. raid
references activist). That intra-domain graph must stay acyclic.
- Business never imports dtos, components, or application. If a domain type needs a wire shape, the DTO depends on the domain - not the reverse.
- Components is infrastructure only: it knows domain types, but not wire
shapes (no
dtos) and not the application layer. - Application is the composition root. It's the only layer allowed to depend
on everything, so it's where wiring and transport live. A common pattern:
service interfaces live in
application/appdeps(a neutral seam) because both the transport handlers and other services consume them.
-
Structure
internal/intodtos/,components/,business/,application/. -
Drop in a short package doc so the layout is documented at the source. Copy
templates/doc.go.tmpltointernal/doc.goand fill in the project's specifics (it links back here for the full spec). The blank import in it is what makes this repo a real, pinned dependency of yours rather than a link in a comment. -
Enforce it mechanically. An inverted import should fail the linter instead of silently rotting the architecture.
With standardgo, reference it:
# .standardgo.yml presets: - github.com/amberpixels/dcba
go get github.com/amberpixels/dcba
The rules come from
standardgo-preset.ymlin this module, at the version yourgo.modpins, with${MODULE}filled in from it. Nothing is copied, so a fix here reaches your project as a version bump.Without standardgo, paste the
linters:block from that same file into your.golangci.ymland replace${MODULE}by hand.
The rule is enforced by depguard today. See the roadmap for the plan to replace the config with a dedicated linter.
dcbacheck- a customgo/analysislinter. A single, configurable analyzer (usable as a golangci-lint plugin/module) that enforces the layering from a tiny per-project config (which package = which layer), so projects stop copying the depguard block. Tracked as an issue in this repo.
DCBA is a solo, opinionated project - but if you stumbled upon it and have ideas, questions, or bug reports, an issue is always welcome :)