Truly Amazing Inversion of control Python library Analogous to .Net's DI
Project description
Taipan-DI
Truly Amazing Inversion of control Python library Analogous to .Net's DI
Taipan-DI is a Dependency Injection library whose goal is to provide a behaviour similar to .Net's DI system.
Features
- Lightweight
- No decorators
- No hidden behaviour (what you write is what you get)
- Automatic dependency injection on service resolving
- Type hinting
- No global container by default
- Singleton and factory scopes
- Register pipelines and chains of responsibility easily
Constraints
- Based purely on types (not on strings)
- No automatic registration
- It is necessary to write an
__init__
function or use@dataclass
Installation
Pip
pip install taipan-di
Poetry
Poetry is a Python dependency management and packaging tool. I actually use it for this project.
poetry add taipan-di
Usage
First, you have to create a DependencyCollection
in which you will register your services. Each DependencyCollection
is independant and contain different services.
services = DependencyCollection()
Then, register your services as you wish. They can be registered as factories or singletons using the following methods :
services.register_factory(InterfaceType, ImplementationType)
services.register_singleton(InterfaceType, ImplementationType)
You can also provide a creator method or an instance (for singletons only) that will be used when resolving the services :
services.register_factory_creator(Type, lambda provider: create(provider))
services.register_singleton_creator(Type, lambda provider: create(provider))
services.register_singleton_instance(Type, instance)
You can also register chains of responsibility and pipelines. Examples are given in the test files.
Once your services are registered, you have to build a dependency provider which will be used to resolve services :
provider = services.build()
resolved = provider.resolve(InterfaceType)
If ImplementationType
has a constructor dependency, it will be automatically resolved, as long as the dependency has been registered in the DependencyCollection
.
Inspirations
This library is partially based on the kink dependency injection library. I was using kink on another project previously but it didn't fit all my requirements.
I also took inspiration from the injector library and .Net's dependency injection system.
TODO
This library isn't stable yet and a lot of things can still be improved. If there is something you want to see added or if something does not work as you want it to, feel free to open an issue.
Here is a list of features I have in mind and will be working on :
- Create configuration from environment or configuration files
Project details
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 taipan_di-0.0.5.tar.gz
.
File metadata
- Download URL: taipan_di-0.0.5.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.16 Linux/5.15.0-1038-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef3c48a02d926b17b1e49c3bc240ca2bed41eb263ce4e8e36e63cb90754361fe |
|
MD5 | a63746479dc6a1dfbadc42c73a84dfac |
|
BLAKE2b-256 | 4ca4f81b68045d31f53a4a1a37980a31d44d0a5256443c5e6d088faf3c1a2158 |
File details
Details for the file taipan_di-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: taipan_di-0.0.5-py3-none-any.whl
- Upload date:
- Size: 16.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.16 Linux/5.15.0-1038-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 446e400ec668ad04b2b762aa49459beb36b92ea74a1939ac2dbb334eb09b265f |
|
MD5 | cd9c7726648567db4ba5e9f467509ab8 |
|
BLAKE2b-256 | bd8c1d7263cdf53d2f0b856781a0fd0f0562d8d1ace325916276311c85db3010 |