Skip to main content

Type-based dependency injection

Project description

logo

Build Status (License MIT 1.0)

enterprython

Python library providing type-based dependency injection

Table of contents

Introduction

If you plan to develop SOLID / domain-driven (i.e., enterprisey) software, you probably want to apply inversion of control in the form of dependency injection when writing the constructors of your classes. Also you likely want to use a library doing the needed lookups for you based on static type annotations, instead of manually configuring the object graph.

enterprython provides exactly that.

from enterprython import assemble, component

@component()
class Service:
    def __init__(self) -> None:
        self._greeting: str = "Hello"

    def greet(self, name: str) -> str:
        return f"{self._greeting}, {name}!"

class Client:
    def __init__(self, service: Service) -> None:
        self._service = service

    def run(self) -> None:
        print(self._service.greet("World"))


assemble(Client).run()

Output:

Hello, World!

Features

Abstract base classes

A client may depend on an abstract base class. Enterprython will inject the matching implementation.

from abc import ABC
from enterprython import assemble, component

class ServiceInterface(ABC):
    ...

@component()
class ServiceImpl(ServiceInterface):
    ...

class Client:
    def __init__(self, services: ServiceInterface) -> None:
        ...

assemble(Client)

One singleton instance of ServiceImpl is created and injected into Client.

Factories

Annotating a function with @factory() registers a factory for its return type.

from enterprython import assemble, component

class Service:
    ...

@factory()
def service_factory() -> Service:
    return Service()

class Client:
    def __init__(self, service: Service) -> None:
        ...

assemble(Client)

service_factory is used to create the Service instance for calling the constructor of Client.

Non-singleton services

If a service is annotated with @component(singleton=False) a new instance of it is created with every injection.

@component(singleton=False)
class Service:
    ...

class Client:
    def __init__(self, service: Service) -> None:
        ...

Service lists

A client may depend on a list of implementations of a service interface.

from abc import ABC
from typing import List
from enterprython import assemble, component

class ServiceInterface(ABC):
    pass

@component()
class ServiceA(ServiceInterface):
    ...

@component()
class ServiceB(ServiceInterface):
    ...

class Client:
    def __init__(self, services: List[ServiceInterface]) -> None:
        ...

assemble(Client)

[ServiceA(), ServiceB()] is injected into Client.

Mixing managed and manual injection

One part of a client's dependencies might be injected manually, the rest automatically.

from enterprython import assemble, component

@component()
class ServiceA:
    ...

class ServiceB:
    ...

class Client:
    def __init__(self, service_a: ServiceA, service_b: ServiceB) -> None:
        ...

assemble(Client, service_b=ServiceB())

service_a comes from the DI container, service_b from user code.

If ServiceB also has a @component() annotation, the manually provided object is preferred.

Free functions as clients

Since class constructors are fundamentally just normal functions, we can inject dependencies into free functions too.

from enterprython import assemble, component

@component()
class Service:
    ...

def client(service: Service) -> None:
    ...

assemble(client)

A singleton instance of Service is created and used to call client.

Requirements and Installation

You need Python 3.6.5 or higher.

python3 -m pip install enterprython

Or, if you like to use latest version from this repository:

git clone https://github.com/Dobiasd/enterprython
cd enterprython
python3 -m pip install .

License

Distributed under the MIT License. (See accompanying file LICENSE or at https://opensource.org/licenses/MIT)

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

enterprython-0.5.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

enterprython-0.5.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file enterprython-0.5.2.tar.gz.

File metadata

  • Download URL: enterprython-0.5.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.8

File hashes

Hashes for enterprython-0.5.2.tar.gz
Algorithm Hash digest
SHA256 05ccfa5e08334572e424d7a6e0ec5c5bed90c865b94cbb3928fb4af0ffaa7be1
MD5 ee3386eda1a0cbfa1f7af36554d84334
BLAKE2b-256 d8e5f69f1232d7ed30cb290d7aaea88f43edbea13f85544563969b88867259a5

See more details on using hashes here.

File details

Details for the file enterprython-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: enterprython-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.8

File hashes

Hashes for enterprython-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e1d29eafb011ca28aae74f497f5a40366e7bbb634b83b81b103be6eecc534674
MD5 81e8172b2fff0139528d861a433920a1
BLAKE2b-256 0a630a4ce6c20509ee1cca8808c28ede5c0a5d0939a958237e3289a46f55a4a5

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