Layer module provides a powerful dependency injection system for building modular, testable Effect applications. Layers describe how to construct services and their dependencies.
Overview
ALayer<ROut, E, RIn> describes how to build services:
ROut: The services this layer providesE: Possible errors during layer constructionRIn: The services this layer requires as dependencies
Creating Layers
Basic Layer from a Service
Layer with Dependencies
Layer Constructors
Layer.succeed
Create a layer that succeeds with a value:Layer.effect
Create a layer from an Effect:Layer.effectDiscard
Create a layer that runs side effects without providing a service:Composing Layers
Layer.provide
Provide dependencies to a layer:Layer.provideMerge
Merge provided layers with the layer’s output:Combining Multiple Layers
Using Layers
Providing Layers to Effects
Layer.launch
Launch a layer as the application entry point:Resource Management
Layer with Cleanup
Layer Memoization
Layers are memoized by default:Testing with Layers
Advanced Patterns
Dynamic Layer Construction
Conditional Layers
Best Practices
- Define layers as static members: Attach layers to service classes
- Use Layer.provide for dependencies: Make dependencies explicit
- Keep layers focused: Each layer should provide a single service or small group of related services
- Use Layer.merge for independent services: Combine services that don’t depend on each other
- Test with mock layers: Replace real implementations with mocks for testing