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:ΒΆ

packages/core/src/flag-journal.ts Β· a3f5a148 Β· Ref.make is a field; FlagJournal.of is the method table
>()("@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:ΒΆ

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:ΒΆ

packages/code-snippets/src/drift.ts Β· a3f5a148 Β· silent needs nothing; layer needs SourceRepository
>()("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;

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

  1. Sam Goodwin - Alchemy - YouTube. Sam Goodwin. Layer composition discussion; used for infrastructure carried with a service. Accessed 2026-10-01.
  2. 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.
  3. 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

  1. 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.ΒΆ

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

  3. Change log. What changed in the files served here, newest first.

    A Layer as constructor β€” new page: Initial commit from joelhooks/rat-stack template

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

  5. 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.ΒΆ

  6. 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.ΒΆ

  7. Full agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
  8. Hexagonal architecture. Ports name the application's jobs; adapters connect them to the outside world.

    A Layer as constructor explains the construction mechanism.ΒΆ

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

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

  11. rat-stack. An Effect stack so pure (aspirational) Kit Langton will blush.
  12. Source change history (log.md). The generated change log as Markdown.

    A Layer as constructor β€” new page: Initial commit from joelhooks/rat-stack template