Release Pager is a sample ZIO application that demostrates many features of ZIO. It is desccribed in three blogs:
- Design
- From idea to product
- Effective testing
1 Design
The application has serveral services:
- The
Chat StorageandRepository Version Storagestore chat and repository version into a database. - The
Subscription Logichandles subscriptions and uses theChat StorageandRepository Version Storageto data persistency. - The
GitHub ClientusesHttp Clientto check versions of a list of GitHub repositories. - The
Repository Validatoruses theGitHub Clientto validate versions. - The
Telegram Clientpages subscribed uses. It usesSubscription LogicandRepository Validator. - The
Release Checkerschedule the services. It usesTelegram Client,Subscription LogicandGithub Client.
The source code is organized by technical layers that form a directed dependency graph: backend -> service -> storage -> domain.
domainhas all the models.storagehas all the data persistency logic.servicehas all business logics.backendhas the logic of starting the server.
The package names are named after business domains, not by techinical layers.
Actually, use business domains to organize soruce code, the technical layers are used only for shared code among doamins.
Another design decision is to use domain types, the the Scala data types. For example, instead of String, the Version and Name types are used for GitHub repository version and repository name.
|
|
Another design decision is to avlid monadic types such as Option[T] as method parameters. Using the simple data type T will make the implementation and testing much easier. Use two methods if a parameter is optional.
This is a good idea. Put more restrictions at the boundary of a system will make the internal much simpler.
2 Code
2.1 Module Pattern
Define a service interface in an object. The ZIO uses Pascal-casing naming for this object.
|
|
The interface methods should only use the inpput/output types. The dependencies are declared in the value of ZLayer that implements the service. The actual service implementation is defined in a case class in a separate file.
The @accessible annotation generates accessor methods in the service object like def method(...): ZIO[R, E, A] = ZIO.accessM(_.get.method(...)). For a service module that is only used in ZLayer.fromServices, there is no need to create accessor methods.
Use a meaningful name instead of Live.
|
|
2.2 Start
The application overrides the the zio.App#run method to run the program.
|
|
It first creates a token and config. Then it migrates database and creates three manages resources: http client, pager client and db transactor. These are used to wire up program dependencies.
ZIO.runtim[R].map { implicit rt => ...} is used to run code that uses resource R. The above code use the ZIO cats interop functions to create managed resources.
2.3 Wiring Up
The logger layer is the Lagger.console. The http client, pager client and db transctor layers are created by the managed resource’s toLayer.orDie method.
The following code creates the dependency layers for sbuscription logic:
|
|
Both vertical and parallel depdencies are composed in the heirarchy. Similarly, repository validator dependency layers are created.
For inMemory storarage, create the dependent layers using ZLayer.fromEffect as the following:
|
|
The start method of the telegramp client and the repeat method of the release check return two effects. The program is described as startTelegramClient.fork *> scheduleRefresh and its dependencies are fulfilled by program.provideSomeLayer[ZEnv](programLayer).