DI module reference¶
- class micro.di.Component(name, bases, dct)[source]¶
- ***
- Deprecated: New code doesn’t need to use this class. Instead, simply declaring a start() method or coroutine is enough to be started.
A metaclass for components which can be injected as dependencies themselves. Components must declare a start() method or coroutine.
- class micro.di.Dep(_type, name=None)[source]¶
A dependency to be injected. Usage example:
@di.has_deps class Depends: mysql = di.Dep(Mysql) def fetch_data(self): self.mysql.run_query(*args) ...
- micro.di.has_deps(cls)[source]¶
Decorate with me the classes that have dependencies. Usage example:
..code:
@di.has_deps class Depends: dep = di.Dep(...)
- micro.di.provide(name_or_class, instance)[source]¶
Provide dependency for name or class to the task local scope.
- class micro.di.DIContext(replace_dict, loop=None)[source]¶
- __enter__(_container=<micro.di.Container object at 0x7f475fa24940>)[source]¶
Enter the provider context.
Parameters: _container (micro.di.Container) – The container to substitute (defaults to global one) Return type: micro.di.Container
- class micro.di.ProviderContext(replace_dict, loop=None)[source]¶
This replaces all current provider with new ones. Useful for applications which need different sets of providers during app lifecycle (for instance, on application finish you can switch some transport component class that incapsulates some remote resource communication with the class which actually cleans the resource connection pool). Most suitable for tests, since there only one test instance running at a time.
- class micro.di.TaskContainerContext(replace_dict, loop=None)[source]¶
This context substitutes providers for task-local context. Only the task where context entering is happened will receive the replace_dict providers; other tasks will have different context
- __enter__(_container=<micro.di.Container object at 0x7f475fa24940>)[source]¶
Enter the task local context, All the new tasks will have the replace_dict installed for provider, for newly created instances in this task will have dependency providers subsituted
Parameters: _container (micro.di.Container) – The container to subsitute (defaults to global one) Return type: micro.di.Container