Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystifying Dependency injection with ZIO 2

Demystifying Dependency injection with ZIO 2

Dependency injection allows developers to have a standardized way of wiring up applications in different ways for different scenarios. While enabling modularity, customizability, and testability, dependency injection has also been criticized for being too magical, too easy to go wrong at runtime, and unnecessary.

In this presentation, Jaro Regec presents several options for dependency injection in Scala, highlighting the strengths and weaknesses of each, and then discusses how you can use ZIO 2 for dependency injection in a best practice way, without overusing ZIO Environment.

Learn how to create modular, customizable, testable code, without boilerplate, and with compile-time failures for missing dependencies, using ZIO 2.

Jaroslav Regec

April 25, 2023
Tweet

More Decks by Jaroslav Regec

Other Decks in Programming

Transcript

  1. AgendA Idiomatic ZIO 2 way 03 Solutions in Scala 02

    What problems are we trying to solve? Dependency Injection 01 DOs and DON'Ts for ZLayers How to structure our ZIO 2 application?
  2. Example Without Dependency injection • Service creates its own dependencies

    • Tight coupling • Dependencies are hidden to the outside world • Hard to test
  3. Dependencies are injected through constructor • Loose coupling of dependencies

    - service does not care how its dependencies are created • Dependencies are not hidden but explicit • Usually comes together with "coding to interfaces" pattern
  4. Wire services at the edge of app • It can

    soon become quite complex and boilerplate-y • Nice to have: ◦ Parallelizing creation of services? ◦ Retrying construction? ◦ Stateful services with 'REF'? ◦ Closing connection pool? • It's just "passing parameters to functions" • Do we need a library for this?
  5. • No autowiring • No lifecycle management • Dependencies are

    visible to the caller of the method • Hard to use with ZIO • Environment type needs to be the same inside for comprehension READER MONAD
  6. • Inferred type is intersection type • Hard to satisfy

    • Missing power of ZIO's .provide() method • Need to run the reader in order to avoid nested Reader READER MONAD
  7. Context Functions • No autowiring • No lifecycle management •

    No type inference • More flexible than reader monad • Less syntactic and runtime overhead
  8. GUICE • Based on runtime reflection & annotations • Not

    typesafe • Easy to use • Minimal boilerplate
  9. mACWIRE • Simple and minimalistic • Based on macros •

    Typesafe - e.g. "Cannot find a value of type: AdvertisementService" • Less powerful than ZLayers
  10. Pros and Cons Reader Monad Context functions Guice Macwire ZLAYER

    in ZIO 2 TypeSafe No Boilerplate Auto Wiring Finalization Parallel construction Constructors as first class values
  11. ACCESSOR METHODS • Unnecessary indirection in code navigation. • Useful

    only in main method. • Confusing - may lead to environment misuse.
  12. Misuse of Environment • Dependencies are exposed to the caller

    of the method • Dependencies are propagated all the way upstream • Discoverability issues • Type inference
  13. Use Constructor for global dependencies • A.K.A Service pattern 2.0

    • Trait + case class implementation • Modular and testable
  14. DEscribe construction of services with ZLAYER • Composable recipes for

    building app • Turns constructor into a value • Equipped with all the operators e.g. map, flatMap, zipPar
  15. Summary • Use constructors ◦ For global (non eliminable) services

    • Use environments ◦ For local eliminable services ◦ Access context at the edge of your application • Use Layers ◦ To describe constructors • Let ZIO autowire • Avoid ◦ Misuse environment for passing services around ◦ Old ZIO 1 patterns & accessor methods
  16. CREDITS: This presentation template was created by Slidesgo, including icons

    by Flaticon, and infographics & images by Freepik THANKS! Questions? [email protected] https://github.com/sviezypan/zio_world_di_talk jaro_regec PS: my colleague loves ZLayers now :)