ZLayer is a ZIO feature which describes a layer in an application. ZLayer makes it easier adding new service. Each layer requires some service as input and produces some service as output which allows to compose multiple layers in terms of dependencies.
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Join the session 5 minutes prior to
the session start time. We start on
time and conclude on time!
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Silent Mode
Keep your mobile devices in silent
mode, feel free to move out of
session in case you need to attend
an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during
the session.
4. ● ZLayer describes a layer of an application
● Every layer in the application requires some service as input RIn and produces some
service as output ROut which may fail with E while producing ROut.
○ A recipe to create service
○ Alternative to Constructors
○ Composable
○ Effectful and Resourceful
○ Asynchronous and non-blocking
Introduction
5. Important Data Types
● ZIO[-R, +E, +A]
● Has
○ Has[A] represents a dependency on a Service type A.
○ binds services to their implementation
○ Combine multiple services together
● ZLayer
○ ZLayer[-RIn, +E, +ROut]
○ Horizontal Composition : layerA ++ layerB
○ Vertical Composition: layerA >>> layerB
6. Defining Services
Main
Controller
Service1 Service2
● Service can be thought of as a
group of functions that deals with
only one concern
● Enables writing more modular code
● Write more testable programs
● Dependency on Interfaces exposed
and not on internal implementation
● For writing services, ZIO provides
Module patterns.
7. Service Pattern
Service Definition (Trait)
Service Implementation (classes
implementing the Trait)
Defining Dependencies
Constructor-based DI
Using ZLayer (as Service Constructor)
8. Module Pattern
Bundling - gives name to the module (package object or some object)
Within the Module object - Wrapping Service definition with Has[ _ ]
datatype (type ModuleName = Has[Service]
Service Definition - define a trait Service that defines the interface
(capabilities)
Service Implementation - define different implementations of service
through ZLayer ( with required dependencies)
Within the Module object, define capability accessors
9. Creating Layers
1. Dependencies
To create a layer, need to indicate what other layers it depends on
e.g. Database Layer needs Socket and Blocking Layers
2. Acquisition/Release Action
How to initialize a service via acquisition action
Also how to release a service - e.g. Database releases connection pool
Some ways to create a ZLayer:
● ZLayer.Succeed - create a layer from a Service or simple values.
● ZLayer.fromFunction - create a layer from a function.
● ZLayer.fromEffect - lifts a ZIO effect to a layer requiring the effect environment.
● ZIO#toLayer - constructs a layer from an effect.
● ZLayer.fromService - builds a layer from a service.
● ZLayer.fromServices - builds a layer from a number of required services.
● ZLayer.fromManaged - lifts any managed resource to zlayer.
10. Dependency Injection
● Building Dependency Graph
○ Compositional Properties
■ Horizontal : layerA ++ layerB (requirements of both layers)
■ Vertical: layerA >>> layerB (requirement of layerA, output of layerB)
● Dependency Propagation
○ provideLayer :
val app:ZIO[Random with Clock, Nothing, Unit] = ???
val effec: ZIO[Any, Nothing, Unit] =
app.provideLayer(Random.live ++ Clock.live)
○ provideSomeLayer :
val app:ZIO[Random with Console with Clock, Nothing, Unit] = ???
val effect: ZIO[Random with Clock, Nothing, Unit] =
app.provideSomeLayer(Console.live)
○ provideCustomLayer :
val app: ZIO[Has[MyService] with Console with Clock, Nothing, Unit] = ???
val effect: ZIO[ZEnv, Nothing, Unit] = app.provideCustomLayer(MyService.live)