Skip to content
amberpixelsPublic

About

Four Go layers (D·C·B·A), one inward dependency rule | Clean/Hexagonal-style architecture convention.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

DCBA

Four layers. One dependency rule. Enforced.

A small, lint-enforced layering convention for Go applications.

License: MIT


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.

The four layers

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)

Why "DCBA" (reverse-alphabetical)?

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.

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.

Consequences worth remembering

  • 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.

Adopting it in a project

  1. Structure internal/ into dtos/, components/, business/, application/.

  2. Drop in a short package doc so the layout is documented at the source. Copy templates/doc.go.tmpl to internal/doc.go and 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.

  3. 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.yml in this module, at the version your go.mod pins, 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.yml and 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.

Roadmap

  • dcbacheck - a custom go/analysis linter. 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.

Feedback

DCBA is a solo, opinionated project - but if you stumbled upon it and have ideas, questions, or bug reports, an issue is always welcome :)

License

MIT © amberpixels

About

Four Go layers (D·C·B·A), one inward dependency rule | Clean/Hexagonal-style architecture convention.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages