Skip to main content

Truly Amazing Inversion of control Python library Analogous to .Net's DI

Project description

Taipan-DI

Truly Amazing Inversion of control Python library Analogous to .Net's DI

Taipan-DI is a Dependency Injection library whose goal is to provide a behaviour similar to .Net's DI system.

Features

  • Lightweight
  • No decorators
  • No hidden behaviour (what you write is what you get)
  • Automatic dependency injection on service resolving
  • Type-checking
  • No global container by default
  • Singleton and factory scopes

Constraints

  • Based purely on types (not on strings)
  • No automatic registration
  • It is necessary to write an __init__ function or use @dataclass

Installation

Pip

pip install taipan-di

Poetry

Poetry is a Python dependency management and packaging tool. I actually use it for this project.

poetry add taipan-di

Usage

First, you have to create a DependencyCollection in which you will register your services. Each DependencyCollection is independant and contain different services.

services = DependencyCollection()

Then, register your services as you wish. They can be registered as factories or singletons using the following methods :

  • services.register_factory(InterfaceType, ImplementationType)
  • services.register_singleton(InterfaceType, ImplementationType)

You can also provide a creator method or an instance (for singletons only) that will be used when resolving the services :

  • services.register_factory_creator(Type, lambda provider: create(provider))
  • services.register_singleton_creator(Type, lambda provider: create(provider))
  • services.register_singleton_instance(Type, instance)

Once your services are registered, you have to build a dependency provider which will be used to resolve services :

provider = services.build()
resolved = provider.resolve(InterfaceType)

If ImplementationType has a constructor dependency, it will be automatically resolved, as long as the dependency has been registered in the DependencyCollection.

Inspirations

This library is partially based on the kink dependency injection library. I was using kink on a project previously but it didn't fit all my requirements.

I also took inspiration from the injector library and .Net's dependency injection system.

TODO

This library isn't stable yet and a lot of things can still be improved. If there is something you want to see added or if something does not work as you want it to, feel free to open an issue.

Here is a list of features I have in mind and will be working on :

  • Register singletons and factories without specifying the interface
  • Create configuration from environment or configuration files

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

taipan_di-0.0.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

taipan_di-0.0.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file taipan_di-0.0.1.tar.gz.

File metadata

  • Download URL: taipan_di-0.0.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.9.16 Linux/5.15.0-1037-azure

File hashes

Hashes for taipan_di-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b14f52c658c79230e8491411721fcfa1402258f3027249536e662f5478710cf2
MD5 4c1d5878482be849895fa63c782d254a
BLAKE2b-256 4af127d5f8b2a5e3a68f426d5f9a7a077e0153281706ed9892315430a8788ca7

See more details on using hashes here.

File details

Details for the file taipan_di-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: taipan_di-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.9.16 Linux/5.15.0-1037-azure

File hashes

Hashes for taipan_di-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7e9608a80c2e6c686b2f96f6e6fa4075542d0b15e53db2c6c0e2820d3e00404
MD5 25c4a67e1f421aee1021e37e1a5e7311
BLAKE2b-256 cabc55350cd033a9eefe2e90c5580c688d7f7a368935aba27f032fc3ff7b3151

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