linkd.context¶
- class Context¶
Type representing a dependency injection context. Used for labelling containers so that they can be accessed using method injection if required. You can create and use your own contexts by creating an instance of this type.
alias of
str
- class ContextRegistry[source]¶
A registry for all DI contexts known to linkd. This acts as a mapping of a context to the type that the container will register itself as when one is created for that context, allowing accessing a specific context’s container from anywhere within a nested DI flow.
It is not absolutely necessary to register a context to a registry in order to use it, however it is required if you want to provide a custom injectable type for that container automatically.
- register(ns: str, linked_type: type[Container] | None = None) Context[source]¶
Create and register a DI context to this registry, along with an optional injectable type.
- Parameters:
ns – The namespace for the new context. This should be an identifier unique to your library/application. For example, linkd uses the root namespace
linkd.contexts.<...extras>for its included contexts.linked_type – The injectable container type for this context. Defaults to
None.
- Returns:
The created context.
- Raises:
KeyError – If a context using the same identifier already exists.
Example
class YourContexts: DEFAULT = linkd.Contexts.DEFAULT FOO = linkd.global_context_registry.register("com.example.contexts.foo")
- type_for(context: Context) type[Container] | None[source]¶
Get the stored type for the given context. This function is used internally when containers are created.
- Parameters:
context – The context to get the type for.
- Returns:
The stored type for the given context, or
Noneif no type was stored, or the context was not registered.
- final class Contexts[source]¶
Collection of the dependency injection context values linkd uses.
- ROOT = 'linkd.contexts.default'¶
The root DI context - ALL other contexts are built with this as the parent.
- class RootContainer¶
Injectable type representing the dependency container for the root context.
alias of
Container
- global_context_registry: ContextRegistry = <linkd.context.ContextRegistry object>¶
The global context registry.