Effect CommunityEC
Effect Community3y ago
2 replies
Joshua

Comparing Design Patterns

Which is better?

// service-file.ts
function getX() {
  return Effect.gen(function* (_) {
    const service = yield* _(FeatureService);
    const http = yield* _(HttpClient);

    return pipe(
      Effect.tryPromise({
        catch: (e) => e as Error,
        try: () => service.getX(),
      }),
      Effect.flatMap(() => {
        return pipe(
          Http.get("/layers"),
          http.fetch
        );
      })
    );
  });
}

// ...some more function in this file

OR
// service-file.ts
export const XRepositoryLive = Layer.effect(
  XRepository,
  Effect.gen(function* (_) {
    const service = yield* _(FeatureService);
    const http = yield* _(HttpClient);

    return {
      getX() {
        return pipe(
          Effect.tryPromise({
           catch: (e) => e as Error,
           try: () => service.getX(),
          }),
          Effect.flatMap(() => {
            return pipe(
              Http.get("/layers"),
              http.fetch
            );
         })
        );
      },
      // ....
    };
  })


For the purpose of this example, all the functions in the first example represent a module i.e a Repository

In the first example, I can call getX and provide FeatureService and HttpClient. I can also use some other function in that file that doesn't require FeatureService without providing FeatureService.

But in the second one, I must always provide FeatureService even if the function I need doesn't require it.
Was this page helpful?