Skip to main content

Dependency injection container for Python3

Project description

pyInjection

pyInjector is a .Net style dependency injection container built for Python3 and utilising the typing module.

  • Register type, instance and lambda functions against a base type for constructor injecting.
  • Supports ABC base types and mock interfaces.
  • Validates container for duplicate registrations, missing registrations and inconsistent scope registrations at runtime.

Table of Contents

  1. Class Setup (for Injectables)
  2. Constructor Setup (for accepting Injectables)
  3. Registration 3.1. Type Registration 3.2. Instance Registration 3.3. Lambda Registration (for decorators)
  4. Resolving 4.1. Lambda Registration with a Resolve 4.2. Resolve for application execution
  5. Validation

Class Setup (for Injectables)

In order for pyInjection to register and inject a class into a constructor, classes must extend a base class. This class can be an abstract class (using ABC) or a mock interface:

from abc import ABC, abstractmethod

class IFoo(ABC):
    @abstractmethod
    def do_something(self) -> None:
	    pass

or

class IFoo:
	def do_something(self) -> None:
		pass

Constructor Setup (for accepting Injectables)

In order for pyInjection to inject objects into class constructors, the constructor must be decorated with @Container.inject:

from pjInjection import Container

class Bar:
	__foo: IFoo

	@Container.inject
	def __init__(self, foo: IFoo):
		self.__foo = foo

	def run(self) -> None:
		self.__foo.do_something()

Registration

pyInjection supports 3 types of injectable registration across both transient and singleton lifetimes.

from pyInjection import Container

Container.add_transient(interface= IFoo, implementation= Foo)
Container.add_singleton(interface= IBar, implementation= Bar)

Container registrations (like in .Net) maps an implementation to the interface base within supported constructors.

Type Registration:

Container.add_transient(interface= IFoo, implementation= Foo)

Instance Registration:

# Note this is registered as transient but will be resolved as a singleton
Container.add_transient(interface= IFoo, implementation= Foo())

Lambda Registration (for decorators):

Container.add_transient(interface= IFoo, implementation= lambda: Foo())

Resolving

In some cases you may wish to resolve an instance from the container (for a lambda function or running the application).

Lambda Registration with a Resolve

...

Container.add_transient(interface= IFoo, implementation= Foo)
Container.add_transient(
    interface= IBar,
    implementation= lambda: Bar(foo= Container.resolve(interface= IFoo)
)

Resolve for application execution

...

Container.add_singleton(interface= IFoo, implementation= Foo)
Container.add_singleton(interface= IBar, implementation= Bar)

bar: IBar = Container.resolve(interface= IBar)
bar.run()

Validation

In order to ensure that the container has been configured correctly and has everything necessary to build all injectable dependencies run the validate method in the composition root:

...

Container.validate()

Any issues found within the container will be raised as an Exception. Exceptions will be raised for the following:

  • Duplicate registrations
  • Missing registrations
  • Type registrations for constructors with primitive types (these should be an instance registration)

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

pyinjection-2.2.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

pyinjection-2.2-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file pyinjection-2.2.tar.gz.

File metadata

  • Download URL: pyinjection-2.2.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for pyinjection-2.2.tar.gz
Algorithm Hash digest
SHA256 a53c773b794a144c57aa9e77f006113110b989b107116ff1739fbdc75f849f95
MD5 ac9ec85153191fbc79238f1cc2c49ae3
BLAKE2b-256 8320956e261db56d71cfcc029fcf0be56ad704366e33080621711dc732c4ff76

See more details on using hashes here.

File details

Details for the file pyinjection-2.2-py3-none-any.whl.

File metadata

  • Download URL: pyinjection-2.2-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for pyinjection-2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fccca3519b7ff131da886de31767920956f411034283c536dc2dcb2b8cf79f32
MD5 f3aa2fed5384699fb1dfa4a023b44168
BLAKE2b-256 b6034da10121a44f80f22e8595e9cd844922e013134a5693ee76faabc2187e4b

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