# Effect Services Without the Ceremony (Learning Effect, Part 8)

- Published: 2026-07-17
- Updated: 2026-10-21
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-8-services-without-the-ceremony/

Dependency injection sounds heavier than it needs to.

Most of the time, the idea is small:

> This code needs a thing. Give it the thing at the edge.

Effect calls the thing a service.

## The plain version

You can pass the thing by hand.

```ts
const fetchUser = (repo: UserRepo, id: number) => repo.findById(id);

const handleRequest = (repo: UserRepo, input: string) => {
  const id = parseUserId(input);
  return fetchUser(repo, id);
};
```

This is honest. The dependency is in the signature.

It also spreads.

Every function between the edge and the database has to accept `repo` and pass it on. Even when it never uses it.

Effect keeps the honesty and drops the passing.

The `UserRepo` below is the same idea, written as a service.

## Name the service

```ts
class UserRepo extends Context.Service<
  UserRepo,
  {
    readonly findById: (id: number) => Effect.Effect<User, UserNotFound>;
  }
>()("UserRepo") {}
```

This is the part where the syntax looks bigger than the idea.

The idea is just:

> There is a service called UserRepo, and this is what it can do.

**Quiz:** What does the service describe?

- What the program needs
- The final success value
- The only possible error

**Answer:** What the program needs — A service describes a dependency the program can ask for.

## Use the service

```ts
const fetchUser = (id: number) =>
  Effect.gen(function* () {
    const repo = yield* UserRepo;
    return yield* repo.findById(id);
  });
```

Now the requirement slot includes `UserRepo`.

The function does not import a concrete database module.

It asks for the service it needs.

## Provide it with a Layer

```ts
const TestUserRepo = Layer.succeed(UserRepo, {
  findById: (id) => Effect.succeed({ id, name: "Ada" }),
});
```

A layer says:

> Here is how to satisfy that service.

**Quiz:** What would you swap in a test?

- The layer that provides UserRepo
- The Effect runtime
- The Success type

**Answer:** The layer that provides UserRepo — Tests can provide a fake layer while the program keeps asking for the same service.

## The mistake: providing too early

It is tempting to provide the real layer inside the helper.

```ts
const fetchUser = (id: number) =>
  Effect.gen(function* () {
    const repo = yield* UserRepo;
    return yield* repo.findById(id);
  }).pipe(Effect.provide(UserRepoLive));
```

The requirement slot goes back to `never`.

That looks tidy.

It is also a trap.

Now the helper picked its own database. A test cannot swap in `TestUserRepo` anymore.

How to spot it: `Effect.provide` anywhere other than the edge.

Provide it where you run it:

```ts
Effect.runPromise(fetchUser(1).pipe(Effect.provide(TestUserRepo)));
```

The test picks the fake. Production picks `UserRepoLive`.

The helper does not care.

## Keep the point small

Services are not a reason to build a giant framework in your app.

They are useful when a dependency is real: database, config, clock, logger, API client.

For a pure helper, skip it.

> **Note:** My rule: if I would not fake it in a test, I probably do not need a service
> for it yet.
