distage is a pragmatic module system for pure-FP Scala & Scala.js. It combines simplicity and expressiveness of pure FP with the flexibility and extreme late-binding, traditionally associated with object-oriented dependency injection frameworks, such as Guice.
The best way get started is to clone
distage-example sample project and play around with it.
It shows how to write an idiomatic
distage application from scratch and how to:
- write tests using
- setup portable test environments using
- create role-based applications
- enable compile-time checks for fast-feedback on wiring errors
- Faster applications and tests:
distageguarantees that no unnecessary instantiations will happen during your tests or application startup.
distageitself is very fast, in part due to not relying on any sort of runtime reflection.
- Quick failure detection:
distageperforms all the integration checks for your application and tests even before any instantiations happened.
- Simple tests:
distageeliminates all the hard work of setting up your test environments, especially configurable ones.
- Better integration tests: distage-testkit allows you to reuse expensive resources (like database connections and docker containers) across multiple integration tests, gaining performance and without sacrificing correctness.
- Simple development workflow: distage-framework allows you to develop Role-Based Applications, a fusion of Microservices and Monoliths, letting you run all your services in one process for development purposes (and even switch to mock implementations with a single commandline argument).
- Easier deployments: Role-based applications allow you to deploy and orchestrate less components and achieve higher computational density.
- Fast compile times and low mental overhead: Unlike fully compile-time DIs,
distagedoes not impose a compile time penalty.
distageextensions are simple to write and do not require type-level programming.
- Simple debugging:
distageprovides you important insights about your application and allows you to introspect and modify it on the fly, before any instantiations happen.
- High Correctness:
distagesupports resources and lifecycle natively and guarantees proper cleanups even when something went wrong.
- No reflection:
distagegenerates constructors and type information at compile-time and does not use Scala reflection. As such, it’s compatible with GraalVM Native Image and Scala.js.
distageis designed to not impact the way your Scala code is written, it just removes all the initialization boilerplate. You don’t need to learn magic tricks to write components in a distage application.
distage is recommended by industry leaders:
Given its native support for type classes and higher-kinded types – both features indispensable to functional programming – distage is one of the leading dependency injection libraries out there. Bonus points for being built by a wicked-smart team that contributes to ZIO!
— John A. De Goes
Q: How to pronounce
Q: Isn’t it unsafe to use runtime dependency injection?
distage is split into two stages, first a wiring
plan is calculated, only afterwards it is executed. Because of this, you can test the
Plan for errors very fast, without executing any effects of your wiring – if tests pass, the wiring will succeed at runtime. Testing can also be performed at compile-time –
distage-framework module a few experimental macros for aborting compilation on planning errors.
Q: How do I switch between production and test implementations of components?
A: Use Activation Axis
- Advanced Features
- Syntax summary
- Hyper-pragmatic Pure FP Testing with distage-testkit
- distage: Staged Dependency Injection
- logstage: Zero-cost Structured Logging
- More slides