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 hinting
  • No global container by default
  • Singleton and factory scopes
  • Register pipelines easily

Constraints

  • Based purely on types (not on strings)
  • No automatic registration

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 ServiceCollection in which you will register your services. Each ServiceCollection is independant and contain different services.

services = ServiceCollection()

Then, register your services as you wish. You can initiate registrations processes via 2 ways :

services.register(Type)
services.register_pipeline(Type)

For "standard" registration, you have to first choose the scope and then how you wish the instances to be created. Examples :

services.register(Type).as_factory().with_implementation(ChildType)
services.register(Type).as_singleton().with_self()
services.register(Type).as_singleton().with_instance(instance)
services.register(Type).as_factory().with_creator(lambda provider: create(provider))

For pipeline registration, all you have to do is add the links that constitute the pipeline and register it as a singleton or a factory. Example :

services.register_pipeline(Type).add(Link1).add(Link2).as_factory()

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

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

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

Inspirations

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

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 :

  • 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.7.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

taipan_di-0.0.7-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: taipan_di-0.0.7.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.17 Linux/5.15.0-1040-azure

File hashes

Hashes for taipan_di-0.0.7.tar.gz
Algorithm Hash digest
SHA256 0903eeae6983cd2aab046f13fa7074f8478469614ca04beef89929e6d1ec165a
MD5 b2081bbfd275a3cf8f06723aeafa77d6
BLAKE2b-256 c8d022eec4d7cda55dfe224ee6d7e0a5aadf7302aa8e1e97f89b8a8854597266

See more details on using hashes here.

File details

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

File metadata

  • Download URL: taipan_di-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.17 Linux/5.15.0-1040-azure

File hashes

Hashes for taipan_di-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 06b715d9c936cb5a9ca22a197d7bb1302afb7d8aed4f9e0672904be04fa9eadd
MD5 4274fbd5fe57d045bff61bd9d8437498
BLAKE2b-256 c7f831b049b109d3dcfbd2d89fb58148ab674a01bf5d456eba07697b441a5669

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