Skip to main content

Constructor injection for Python

Project description

diapyr

Constructor injection for Python

Overview

  • Automatic wiring between application objects
  • Declare what collaborator types you want in your constructor using an @types decorator, they correspond with params
    • You can decorate a (factory) function in the same way, in which case the return type must also be declared using 'this' kwarg
    • Surround a type in square brackets if you want a list of all matching objects, normally diapyr provides a unique match
  • Add such classes/factories to a DI instance
    • You can also add objects, for example an application config object
  • Request a type from the DI instance and diapyr will attempt to make it for you, along with the rest of the object graph
  • Instances are cached in the DI object
    • On exit from 'with' clause, dispose is called on any created instances that have it

Motivation

  • Manual wiring is messy and tedious especially when an app gets big
  • Constructor injection avoids spaghetti code
  • It's trivial to depend on an object anywhere in the graph as long as you don't create a reference cycle e.g. most objects will depend on the same config instance
    • No need to resort to globals, which come with a big risk of leaking state between unit tests
  • Unit testing is easy when an object only interacts with what was passed into its constructor

Convention

  • When depending on a config object the constructor should first extract what it needs and assign those values to fields
  • Collaborators should also be assigned to fields, and ideally the constructor won't do anything else

Advanced

  • Parameter defaults are honoured, this can be used to depend on module log object in real life and pass in a mock in unit tests
  • Decorating an instance method (without 'this' kwarg) will make it behave as an additional constructor
    • Take advantage of name mangling (start with double underscore e.g. __init) to avoid having to call super
  • Decorating an instance method with 'this' kwarg will make it behave as a factory function
    • Adding a class to DI will implicity add all such methods it has as factories
  • You can play fast and loose with types, diapyr doesn't care whether a factoried object satisfies the declared type

Install

These are generic installation instructions.

To use, permanently

The quickest way to get started is to install the current release from PyPI:

pip3 install --user diapyr

To use, temporarily

If you prefer to keep .local clean, install to a virtualenv:

python3 -m venv venvname
venvname/bin/pip install diapyr
. venvname/bin/activate

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

diapyr-28.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

diapyr-28-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file diapyr-28.tar.gz.

File metadata

  • Download URL: diapyr-28.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.18 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9

File hashes

Hashes for diapyr-28.tar.gz
Algorithm Hash digest
SHA256 92d637bc5f92fe7838791b3997b25598ca9b9975615bc76a8f7e44ab1b69dc85
MD5 be9c3f31e283fe71f5e671953ea49b5b
BLAKE2b-256 fa47f06b0ae91ac1a380645a268d8529738b639981e3c8325108c718b0eaf2e6

See more details on using hashes here.

File details

Details for the file diapyr-28-py2.py3-none-any.whl.

File metadata

  • Download URL: diapyr-28-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.18 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.9

File hashes

Hashes for diapyr-28-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 543842855e934bb9f7aa3c760779d745824ae6c0796bb00f5cb3911e4f7a1566
MD5 b453f1908d32cb54be07cc6f7e02bd9b
BLAKE2b-256 833867ef3087f803ab04368a9d94bcbc9fa0b5e35fd66beec9e9addcbb689371

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