Skip to main content

Dependency Injection made simple.

Project description

Dependency Injection made simple.

Instalation

pip instal simple-injector

Getting Started

from simple_injector import SimpleInjector, inject
from random import random

class Repository():
    def __init__(self):
        self.number = random()

    def get_a_number(self):
        return self.number

class Service():
    def __init__(self, repo: Repository):
        self.repo = repo

    def get_number(self):
        return {"n":self.repo.get_a_number()}

def bootstrap():
    SimpleInjector().register(Repository)
    SimpleInjector().singleton(Service)

bootstrap()

repo = inject(Repository)
serv = inject(Service)

Reference Guide

SimpleInjector()

This will always return the same instance of the SimpleInjector.

SimpleInjector().register(class, obj_or_callable=None)

Used to register a class on the dependencies list.

Can also receive an instance of the object that you want to assign to it.

Can also receive a callable with one parameter, that will be the instance of the Reference.

Can also receive a callable with 0 parameters.

Any lambda received as a parameter should always return the instance that will be injected.

SimpleInjector().singleton(class)

Used to register a class as a singleton on the dependencies list.

This class' dependencies will be resolved at the moment of this method call, so all its dependencies must be already registered with SimpleInjector.

SimpleInjector().lazy(class, obj)

Used to register a class with its instantiate object on the dependencies list.

Any call to SimpleInjector().resolve(class) to this class will be resolved with the object previously provided.

SimpleInjector().resolve(class) or inject

Used to resolve a dependency of a class that has been registered prior to the execution of this resolve method.

This method will return an object it has on the dependency list for this class, if it is a singleton, or a registered object, otherwise it will return a new instance for the class provided, injecting all needed dependencies, if they were registered prior to the execution of this method.

SimpleInjector().instantiate(class, extraParams={})

Can be used if you need to get a new instance of some class, or if you want a new instance and want to override some param on the constructor of the class.

SimpleInjector().reset()

Can be used if you need to be sure that the SimpleInjector has no dependencies registered on its list

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

simple-injector-1.1.2.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

simple_injector-1.1.2-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file simple-injector-1.1.2.tar.gz.

File metadata

  • Download URL: simple-injector-1.1.2.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for simple-injector-1.1.2.tar.gz
Algorithm Hash digest
SHA256 897addcb04dc9b2a0661c0e4fededb8da8ab78b3a7cd1998f4630ef655f65220
MD5 1e67a8d2b3b530a67141174c770cd46f
BLAKE2b-256 1382a0ed6dd1624e52b9e24f05f1468c69c440bee96658c1362f007d8bd6d4ed

See more details on using hashes here.

File details

Details for the file simple_injector-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: simple_injector-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for simple_injector-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3e39ff3746c589abd30dc92bd640c2c993aaf358ddce3290c2b79f80ed11ffd6
MD5 93388904036371ead34f7dbdae4a60c2
BLAKE2b-256 184f93d352dd200fa9432f42afe7034247adbb13ba20ca51cca23c05c431d80e

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