Skip to content

Accessors considered harmful #4

@jpowersdev

Description

@jpowersdev

accessors: true,

Hey Makisuo,
Just wanted to make a quick suggestion - I'd recommend not using accessors: true in your service definitions. When you directly use FooService.method inside of BarService.method, it makes BarService.method dependent on FooService. What you probably want instead is for services to depend on each other. i.e.

class FooService extends Effect.Service<FooService>()("FooService", {
  accessors: true,
  effect: Effect.gen(function*(){
    return {
      method: Effect.fn("FooService.method")(function*(){
        yield* Effect.log("Hello")
      })
    } as const
  })
}) {}

class BarService extends Effect.Service<BarService>()("BarService", {
  dependencies: [FooService.Default],
  effect: Effect.gen(function*(){
    const fooService = yield* FooService
    return {
      method: Effect.fn("FooService.method")(function*(){
        yield* fooService.method()
      }), // this method has no requirements, as the layer constructor takes care of them
      methodWithAccessor: Effect.fn("BarService.methodWithAccessor")(function*(){
        yield* FooService.method()
      }) // this method has a requirement which will have to be satisfied later
    } as const
  })
}) {}

Just thought this might help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions