A Layer as constructor
Goal: write a Layer21 the way you would write a class constructor.ΒΆ
A Layer describes how to build a service30 and supply its dependencies. A service is the named interface26 callers use for a job.ΒΆ
Worked example: a journal with private state
FlagJournal records flag changes.
Its memory Layer is a constructor:ΒΆ
>()("@rat-stack/core/FlagJournal") {
static memory = Layer.effect(
FlagJournal,
Effect.gen(function* makeFlagJournal() {
const entries = yield* Ref.make<readonly FlagChange[]>([]);
return FlagJournal.of({
list: Effect.fn("FlagJournal.list")(function* list(input) {Read it as a class:ΒΆ
Layer.effect(FlagJournal, β¦)is the constructor forFlagJournal.ΒΆRef.make([])creates a private field. Only the returned methods can reachentries.ΒΆFlagJournal.of({ β¦ })returns the methods, checked against the service interface.ΒΆ
Each build of the Layer runs the constructor once and creates a fresh entries list.ΒΆ
One service, two constructors
A class can have more than one constructor. A service can have more than one Layer. Drift checks pinned code excerpts against the current source:ΒΆ
>()("code-snippets/Drift") {
static readonly silent = Layer.succeed(
Drift,
Drift.of({ check: () => Effect.succeed([]) })
);
static readonly layer = Layer.effect(
Drift,
Effect.gen(function* makeDrift() {
const repository = yield* SourceRepository;Drift.silentusesLayer.succeed. It needs nothing and reports nothing.ΒΆDrift.layerusesLayer.effect. Its constructor asks forSourceRepository, so the Layer carries that requirement35.ΒΆ
Callers ask for Drift either way. The composition root18 picks the constructor.ΒΆ
Why it matters
A constructor argument becomes a Layer requirement. The type shows it, and the root must supply it.ΒΆ
Sam compares the constructor pattern to a Layer and uses a React component as an analogy. In the same discussion, he connects the pattern to infrastructure policies. Watch from 18:45ΒΆ
Next
Read Layers make dependencies explicit21 to compose several Layers. Then read why dependency injection matters14.ΒΆ
See Sam Goodwin on Alchemy10 and cartridges38.ΒΆ
Sources
- Sam Goodwin - Alchemy - YouTube. Sam Goodwin. Layer composition discussion; used for infrastructure carried with a service. Accessed 2026-10-01.
- Flag journal service. GitHub joelhooks/rat-stack. FlagJournal.memory builds private state with Ref.make and returns methods with FlagJournal.of. Accessed 2026-11-08.
- Source drift service. GitHub joelhooks/rat-stack. Drift has two Layers: silent needs nothing, and layer needs SourceRepository. Accessed 2026-11-08.
Last changed 2026-11-08 in fa39fa0. Source on GitHub. Change log.
Linked from
- A service captures its construction dependencies. Build a service with its dependencies, then expose methods that callers can use without rebuilding the graph.
See also: domain structure, Layer composition, the Layer constructor pattern, and testing through services.ΒΆ
- Agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
A Layer as constructor β Sam Goodwin compares an Effect Layer that builds a service to a constructor.
- Change log. What changed in the files served here, newest first.
A Layer as constructor β new page: Initial commit from joelhooks/rat-stack template
- Code snippets quote a pinned source. Shipped design brief: pinned Git objects, typed diagnostics, and one build-only snippet model for both views.
Layers provide the adapters.
- Dependency injection is the reason to choose Effect. Effect tracks service requirements in types and supplies replaceable implementations through Layers.
Read the Layer constructor pattern.ΒΆ
- Engine-neutral tokens keep adapters replaceable. The highlighter port exposes text, roles, and font styles rather than Shiki types.
Both adapters provide the same port through an Effect Layer.ΒΆ
- Full agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
- Hexagonal architecture. Ports name the application's jobs; adapters connect them to the outside world.
A Layer as constructor explains the construction mechanism.ΒΆ
- learn-rat-stack. Trace a capability through Effect, XState, five projections, the CLI, and Alchemy. Try hosted search and read.
The service exposes its Layer beside the implementation.
- Rat Stack lore | rat-stack. Short, source-grounded notes on the ideas and decisions behind rat-stack.
A Layer as constructor β Sam Goodwin compares an Effect Layer that builds a service to a constructor.
- rat-stack. An Effect stack so pure (aspirational) Kit Langton will blush.
- Source change history (log.md). The generated change log as Markdown.
A Layer as constructor β new page: Initial commit from joelhooks/rat-stack template