Skip to main content

No project description provided

Project description

simple-dependency-injection

Quality Gate Status Coverage Production Pipeline

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

simple-dependency-injection-0.1.6.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file simple-dependency-injection-0.1.6.tar.gz.

File metadata

  • Download URL: simple-dependency-injection-0.1.6.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.14 Linux/5.4.0-1091-azure

File hashes

Hashes for simple-dependency-injection-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1e527b260fce7e09ab2b36dc710a87f97df061407492ac307d266a20aa9186c5
MD5 c3e73ae7ec6a9f863b2d76ab9185c383
BLAKE2b-256 214c149e391696fd90e901b54dca7341f61ee76f03911ae3de4b5d9d3c331fe1

See more details on using hashes here.

File details

Details for the file simple_dependency_injection-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_dependency_injection-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fe54df35b9f349e590962c1979d836d1a16ff3eb2309dfda091d7d32c66d4d1e
MD5 2420302bf92815827b9cf0cc29a1b542
BLAKE2b-256 1eadddd6dde396c8aa9d98f331a8759d33ada7ba13f6ec2f260ddd53845d50d8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page