Service-Oriented Architecture
Effect services are the most common way to structure Effect code. Prefer using services to encapsulate behavior over other approaches, as it ensures that your code is modular, testable, and maintainable.Defining Services with ServiceMap.Service
The default way to define a service is to extendServiceMap.Service, passing in the service interface as a type parameter.
Service Layers with Layer.effect
Layers define how services are constructed and their dependencies. UseLayer.effect to create a layer from an Effect that builds the service.
Composing Services with Layer
Layer.provide
UseLayer.provide to satisfy dependencies and expose only the top-level service:
Layer.provideMerge
UseLayer.provideMerge to expose both the service and its dependencies:
ServiceMap.Reference for Configuration
UseServiceMap.Reference for defining configuration values, feature flags, or any other service that has a default value.
Dynamic Layer Construction with Layer.unwrap
Build a layer dynamically from an Effect or Config withLayer.unwrap:
Best Practices
- Use
ServiceMap.Serviceto define service interfaces - Use
Layer.effectto implement service layers - Define service methods with
Effect.fnfor better tracing - Use
Layer.provideto hide implementation details - Use
Layer.provideMergewhen you need to expose dependencies - Use
ServiceMap.Referencefor configuration values with defaults - Use
Layer.unwrapfor dynamic layer construction based on configuration - Name service identifiers with package name and path (e.g., “myapp/db/Database”)