Dependency Injection Container for Python
Project description
Dependency Inection Container for Python
This is a basic Dependency Inection Container for Python.
How to use it
At the moment, ditainer
can only load dependencies from yaml files.
A dependency is treated as a service. A service is a class and could receive arguments to be initialized. It is also possible to initialize a class through its factory, which may also require arguments. Additionally, a service can be tagged.
Types of arguments:
- Simple. Just the literal argument.
- A reference to another service. The prefix
!ref
followed by the id of the other service. - All services tagged with a specific tag. The prefix
!tagged
followed by the tag.
An example of yaml file could be:
services:
- id: DBSession
module: my_module.db
class: DBSession
factory: create_session
- id: UserRepository
module: my_module.user.repository
class: MySqlUserRepository
arguments:
- !ref DBSession
- id: QueryBus
module: my_module.query_bus
class: InMemoryQueryBus
arguments:
- !tagged query_handler
- id: UserFindByIDQueryHandler
module: my_module.user.find_by_id_query_handler
class: UserFindByIDQueryHandler
arguments:
- !ref UserRepository
tags:
- query_handler
- id: UserFindByCodeQueryHandler
module: my_module.user.find_by_code_query_handler
class: UserFindByCodeQueryHandler
arguments:
- !ref UserRepository
tags:
- query_handler
Each service must have id, module and class. If the service has no id, module or class, there will be an error.
ditainer
can also understand an import file with different files to load:
imports:
- { resource: "./services_1.yaml" }
- { resource: "./services_2.yaml" }
- { resource: "./services_3.yaml" }
Start using ditainer
Once the yaml files are ready:
import os
from ditainer.container import Container
from ditainer.loader import YAMLLoader
container = Container()
loader = YAMLLoader(container)
loader.load(os.path.join(os.path.dirname(__file__), "imports.yaml"))
from my_module import container
from my_module.user import User
from my_module.user.find_by_id_query import UserFindByIdQuery
def get_user() -> User:
query_bus = container.find("QueryBus")
query = UserFindByIdQuery(123)
user = query_bus.ask(query)
return user
It is also possible to find services by tags:
from my_module import container
from my_module.user import User
from my_module.user.find_by_id_query import UserFindByIdQuery
query_handlers = container.find_tagged("query_handler")
You can find more examples in the tests folder.
Based on: https://www.npmjs.com/package/node-dependency-injection
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ditainer-1.1.0.tar.gz
.
File metadata
- Download URL: ditainer-1.1.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7dde16143019e040ad529964840416d9a1e8c96bbaffe446c1cb148cd240da2 |
|
MD5 | da59ce68033ddd1d5ec80e348b433c87 |
|
BLAKE2b-256 | 7e9ffd74ec8c391afb8402e62ea94dfcd8637385baa6c4f7de65aa09a0cd2863 |
File details
Details for the file ditainer-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: ditainer-1.1.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a4824bfe3d0c0a725589986dcb924f6dd6b81fd901396445ed7f539be50fe1c |
|
MD5 | ee298eed5cf169a12bc5570ed10f4d8a |
|
BLAKE2b-256 | d5ba58a7351350533640aa63abeddfb498caedb36792916b1233df29be720f77 |