Context holds service implementations for dependency injection. It's an immutable container that maps Tags to their implementations.
const ctx = Context.empty() .add(Logger, consoleLogger) .add(Database, pgDatabase)// Access a serviceconst logger = ctx.get(Logger) // Option<Logger>const loggerUnsafe = ctx.unsafeGet(Logger) // Logger (throws if missing) Copy
const ctx = Context.empty() .add(Logger, consoleLogger) .add(Database, pgDatabase)// Access a serviceconst logger = ctx.get(Logger) // Option<Logger>const loggerUnsafe = ctx.unsafeGet(Logger) // Logger (throws if missing)
The services contained in this context (intersection type)
Readonly
Returns the number of services in this context.
Adds a service to the context, returning a new context.
The tag for the service
The service implementation
A new context with the service added
Gets a service from the context.
The tag identifying the service
Some(service) if found, None otherwise
Checks if a service exists in the context.
The tag to check
true if the service exists
Merges another context into this one.
The context to merge
A new context with all services from both
String representation
Gets a service from the context, throwing if not found.
The service
Error if service not found
Context holds service implementations for dependency injection. It's an immutable container that maps Tags to their implementations.
Example