Skip to main content

A simple OOP Dependency injection container

Project description

Intro

Dependency Injection is the key pattern used to implement Inversion of Control and Dependency Inversion principles (check D in SOLID).

It makes it simple to standardize the way objects are initialized in the application.

This package provides you with the set of components to automate dependency injection through the application.

Installation

Add oop-di as a dependency in pyproject.toml of your project. Or, do

pip install oop-di

Usage

To make it work do the following steps:

  • provide definitions for all the services
  • compile the container
  • use container to inject services
# Init the `ContainerDefinition` component
container_definition = ContainerDefinition()
# fill the service definitions
container_definition.add_service(SomeService)
# compile the container
container = container_definition.compile()

# get an instance of the service from the container ...
service = container.get(SomeService)

# ... or inject it. Kwonly arguments will be injected
@container.inject()
def something(*, service: SomeService):
    ...

something()

# partial injection is also possible
def something_else(*, wont_inject: UnknownService, service: SomeService):
    ...

something_else(wont_inject=UnknownService())

Extensions

EnvExtension

You can add all environment variables to your container by adding EnvExtension to the container definition:

container_definition = ContainerDefinition()
container_definition.add_extension(EnvExtension())

JsonExtension

You can add define parameters/aliases/services in a JSON file. Check examples/s4_json.py for more details.

container_definition = ContainerDefinition()
container_definition.add_extension(JsonExtension(Path(__file__).parent / "config.json"))

Config example:

{
  "parameters": {
    "test1_email": "test@example.com",
    "test2_email": "test2@example.com",
    "test3_email": "test3@example.com"
  },
  "services": {
    "examples.s4_module": [
      {"class": "ProductService"},
      {"class": "Mailer", "name": "m1","tags": ["admin_mailers"], "parameters": {"from_email": "test1_email"}},
      {"class": "Mailer", "name": "m2","tags": ["admin_mailers"], "parameters": {"from_email": "test2_email"}},
      {"class": "Mailer", "name": "m3","tags": ["admin_mailers"], "parameters": {"from_email": "test3_email"}},
      {"class": "MultiMailer", "name": "@examples.s4_module.MailerInterface","parameters": {"mailers": "#admin_mailers"}}
    ]
  }
}

Why not python-dependency-injector?

python-dependency-injector is the most popular Python DI library with a large community, excellent docs, and tons of features that are not available in oop-di. Probably, python-dependency-injector is the best choice for you. I've built this small library to handle the DI of my small experiments/pet projects.

What I don't like in python-dependency-injector and tried to mitigate in this library:

  • it requires a name for every service. oop-di can use class itself as the name of the service;
  • it makes you pollute your function's params with = Provide[Container.service] defaults to inject something. oop-di just auto-wires the services by checking the parameter's types;
  • oop-di has the tagging feature that can gather services into an array without an explicit definition of that array.

Check examples/ folder for more info:

  • s1_simple.py - shows the basics: how to define services and how to inject the services somewhere.
  • s2_extensions.py - how to split the definitions between modules.
  • s3_tags.py, s3_tags2_dict.py - how to use tags to group services (list or dict grouping is possible).
  • s4_json.py - JsonExtension usage example.

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

oop_di-0.6.0.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oop_di-0.6.0-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file oop_di-0.6.0.tar.gz.

File metadata

  • Download URL: oop_di-0.6.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for oop_di-0.6.0.tar.gz
Algorithm Hash digest
SHA256 2b316258197380a5249e7d4b07a597069e75da698ab08840eff65edb338be469
MD5 ece39339bb5b01b11cbeb06c8a7c3961
BLAKE2b-256 2f1ee63685df44dd6cd1a5fac6def76a7c395ce3ec614c4dcb83b7df6275ced3

See more details on using hashes here.

File details

Details for the file oop_di-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: oop_di-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.12

File hashes

Hashes for oop_di-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 65ec01102bea414929c3c9409b9ae85e5c140afdede8fae70431e1a8e35dc747
MD5 a32face3fdb69a1869c0a6aab856036a
BLAKE2b-256 37cfc327807477714c5b1646670016bed6fa0be91ec7588b0f89d5b827ee1bd4

See more details on using hashes here.

Supported by

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