sidnt / zionotes

⸮ 🔴zio notes | zio nursery 🔵?
0 stars 0 forks source link

When is the module pattern helpful #26

Open sidnt opened 2 years ago

sidnt commented 2 years ago

what does module pattern help us achieve? it basically helps create an indirection between the structure of the api (of a service) and the implementation of the api (of that same service), so in essence, we can have multiple implementations of the same api. that is the speciality of module pattern. the ability to program against an interface, and have the freedom to provide multiple implementations, also multiple implementations that can differ by the differences in between their respective dependencies. so even though we can hardcode the api-tl-dependencies in the api structure itself, eg

trait someService:
  def theMethod(/* its params*/): ZIO[HardCodedDependencyType, SomeError, SomeSuccessValue]

but we don't have to, eg

trait someService:
  def theMethod(/* its params*/): ZIO[Any, SomeError, SomeSuccessValue]
  //  iow, def theMethod(/* its params*/): IO[SomeError, SomeSuccessValue]

so module pattern helps us achieve something. but do we always organize all of our zio code, through and through, via the module pattern, esp in cases, like suppose we just have a

def hi = printLine("hello")
def bye = printline("bye")

but we can also have, via the module pattern

trait Greet:
  def hi: IO[IOException, Unit]
  def bye: IO[IOException, Unit]

object Greet

//TBC