This small library is intented to direct copy into your project.
trait DB
object DB {
val live: Layer[Any, DB] = Layer.resource {
makeRes("db").as(new DB {})
}
}
trait HttpCli
object HttpCli {
val live: Layer[Any, HttpCli] = Layer.resource {
makeRes("httpClient").as(new HttpCli {})
}
}
trait AClient
object AClient {
val live: Layer[HttpCli, AClient] = Layer.function { (a: HttpCli) =>
makeRes("AClient").as(new AClient {})
}
}
trait Service1
object Service1 {
val live: Layer[DB & HttpCli, Service1] = Layer.function {
(db: DB, http: HttpCli) =>
IO(new Service1 {}).toResource
}
}
trait Service2
object Service2 {
val live: Layer[DB & AClient, Service2] = Layer.function {
(db: DB, aclient: AClient) =>
IO(new Service2 {}).toResource
}
}
trait App
object App {
val live: Layer[Service1 & Service2, App] = Layer.function {
(s1: Service1, s2: Service2) =>
makeRes("app").as(new App {})
}
}val appLayer: Layer[Any, App] =
(DB.live ++ (HttpCli.live >+> AClient.live)) >>> (Service1.live ++ Service2.live) >>> App.live // Munally construction
val appLayer = Layer.make[App]( // Auto construction
DB.live,
HttpCli.live,
AClient.live,
Service1.live,
Service2.live,
App.live
)appLayer.build(ZEnv(())).use { env =>
IO.println(env.get[App])
}