No project description provided
Project description
simple-dependency-injection
simple-dependency-injection is a library is lightweight library to apply dependency injection pattern in a simple way.
Note: simple-dependency-injection is in a development state, there were some checks to finish the first version
Install
You can install it through pip
pip install simple-dependency-injection
Use
To use simple-dependency-injection only have to create a dependency container and register your dependencies.
The library check types of parameters and result, its important typing that.
That is an example of dependency container configuration
from abc import ABC, abstractmethod
from simple_dependency_injection.dependency_container import DependencyContainer
class ConfigDependencyInterface(ABC):
@abstractmethod
def get_percent_benefit(self) -> float:
pass
class ConfigDependency(ConfigDependencyInterface):
def get_percent_benefit(self) -> float:
return 10.0
def config_generator() -> ConfigDependencyInterface:
return ConfigDependency()
class ServiceInterface(ABC):
def __init__(self, config: ConfigDependencyInterface):
self.config = config
@abstractmethod
def calculate_benefit(self, amount: float) -> float:
pass
class Service(ServiceInterface):
def calculate_benefit(self, amount: float) -> float:
return amount * (self.config.get_percent_benefit() / 100)
def service_generator(config: ConfigDependencyInterface) -> ServiceInterface:
return Service(config=config)
dependency_container = DependencyContainer()
dependency_container.register_dependency(
ConfigDependencyInterface, config_generator
)
dependency_container.register_dependency(
ServiceInterface, service_generator
)
Once the dependency container is created, you can use it with inject decorator.
@dependency_container.inject
def main(service: ServiceInterface):
service.calculate_benefit(10)
main()
Other way to use the dependency container is get the dependency directly
dependency_container.get_dependency(ServiceInterface).calculate_benefit(10)
For testing is easy override dependencies
class ConfigToTestDependency(ConfigDependencyInterface):
def get_percent_benefit(self) -> float:
return 0.0
def test_config_generator() -> ConfigDependencyInterface:
return ConfigToTestDependency()
with dependency_container.test_container() as dependency_container_test:
dependency_container_test.override(
ConfigDependencyInterface, test_config_generator
)
main()
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 simple-dependency-injection-0.1.5.tar.gz
.
File metadata
- Download URL: simple-dependency-injection-0.1.5.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.8.13 Linux/5.4.0-1086-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21b3c32b00f060a9adf9fb0e418ada9695cc49fc7e129a12519e80bb94003eb5 |
|
MD5 | 115a701231b725793b1b2fe0951a5ffd |
|
BLAKE2b-256 | 6839a516a8e42e214a8bb9b25ca14db8729da147d78193009483f0e30078c2e1 |
File details
Details for the file simple_dependency_injection-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: simple_dependency_injection-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.8 CPython/3.8.13 Linux/5.4.0-1086-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6b0cc9cece72e4e182cca9f83ddccf1647733c20fe3a37378d6c5382f38a8d4 |
|
MD5 | 9bfe6fd0eeca9625854720cf8ef187a1 |
|
BLAKE2b-256 | bba15171807ea2c12c52785e69f3f7fca9c3f9f7373aa760582f910080150641 |