Skip to content

"Service pattern 2.0", how to "hide" implementation specific dependencies from service interface #7147

Discussion options

You must be logged in to vote

@artur-jablonski You want to express A as a dependency of your service implementation like the below.

object workflowThatNeedsA {
  val layer: ZLayer[Any, Nothing, A] = ???

  def doStuff: ZIO[A, Nothing, Unit] = ???
}

trait MyService {
  def makeMeRich: ZIO[Any, Nothing, Unit]
}

object MyService {
  def makeMeRich: ZIO[MyService, Nothing, Unit] =
    ZIO.serviceWithZIO[MyService](_.makeMeRich)
}

case class MyServiceImpl(a : A) extends MyService {
  override def makeMeRich: ZIO[Any, Nothing, Unit] =
    workflowThatNeedsA.doStuff.provideEnvironment(ZEnvironment(a))
}

object MyServiceImpl {
  val layer: ZLayer[A, Nothing, MyService] = ZLayer.fromFunction(MyServiceImpl(_))
}   

You will…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@artur-jablonski
Comment options

Answer selected by artur-jablonski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants