Last active
June 4, 2020 05:40
-
-
Save adamw/6cea7ed2afe675ffab0e7bf7e22b1c2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "[email protected]"))) | |
// 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