Road To ZIO[n]: chapter 2, Types in the Domain layer

Manuel Rodríguez
1 min readMar 23, 2022

--

Hi there!

note: this is a series of posts. First one is Road to ZIO[n]: chapter 1, the basics. Code is stored in THIS REPO

In this chapter of my process of creating a ZIO application I have been working in the domain layer. To do so, I’ve followed what I introduced in the article “Smart constructors in Scala” and “Refined Types in Scala: the Good, the Bad and the Ugly”. The idea here is that our Types in the Domain layer represent our business logic as accurately as possible.

Our first model for the users was just “case class User(id: Int, name: String)”. But do we want to allow a negative id? Or an empty name? I’ve imposed such restrictions in Type system and create a couple functions to manage them

With this we can change how the user is modeled. We want to restrict how the users are created, making sure that they follow these restrictions. So we have:

How can we use this in our app? As you can see, this makes the logic a bit more complex but provides reliability on the result

This has the drawback of making the classes using the Domain layer a bit more complex. For now I’ve made quite a dirty code on those layers, but on future chapters we’ll see how to deal with that.

--

--

Manuel Rodríguez

Developer at New Relic. This is where I keep the cool stuff that I learn in my free time