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

By Esteban Trillo2 min readupdated Learning EffectEffectTypeScript

Same program, same slot. Plug in the real repo or the test one.
Same program, same slot. Plug in the real repo or the test one.

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.

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

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.

What does the service describe?

Use the service

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

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

A layer says:

Here is how to satisfy that service.

What would you swap in a test?

The mistake: providing too early

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

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:

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.

Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.