Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active June 4, 2020 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/6cea7ed2afe675ffab0e7bf7e22b1c2c to your computer and use it in GitHub Desktop.
Save adamw/6cea7ed2afe675ffab0e7bf7e22b1c2c to your computer and use it in GitHub Desktop.
import zio._
object Main extends App {
override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] = {
// instead of a method accessor, explicitly accessing the environment
val program: ZIO[Has[UserRegistration], Throwable, User] =
ZIO.accessM[Has[UserRegistration]](_.get.register(User("adam", "adam@hello.com")))
// the DB service is created using through layers (which wrap managed resources)
val dbLayer: ZLayer[Any, Throwable, DB] =
ZLayer.succeed(DBConfig("jdbc://localhost")) >>>
ConnectionPoolIntegration.live >>>
DB.liveRelationalDB
// the UserRegistration service graph is created using construtors
val userRegistrationLayer: ZLayer[Any, Throwable, Has[UserRegistration]] =
dbLayer.map { db =>
lazy val userModel = new DefaultUserModel(db.get)
lazy val userRegistration = new UserRegistration(DefaultUserNotifier, userModel)
Has(userRegistration)
}
program
.provideLayer(userRegistrationLayer)
.catchAll(t => ZIO.succeed(t.printStackTrace()).map(_ => ExitCode.failure))
.map { u =>
println(s"Registered user: $u (layers)")
ExitCode.success
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment