Dependency Injection made simple.
Project description
Dependency Injection made simple.
Instalation
pip instal simple-injector
Getting Started
from simple_injector import SimpleInjector
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().register_singleton(Service)
bootstrap()
repo = SimpleInjector().resolve(Repository)
serv = SimpleInjector().resolve(Service)
Reference Guide
SimpleInjector()
This will always return the same instance of the SimpleInjector.
SimpleInjector().register(class)
Used to register a class on the dependencies list.
SimpleInjector().register_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().register_instance(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)
Used to resolve a dependency of a class that has been registered prior to the execution of this resolve method. This method will return any 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple-injector-1.0.2.tar.gz.
File metadata
- Download URL: simple-injector-1.0.2.tar.gz
- Upload date:
- Size: 2.9 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69a31225ff03e538cb5954601a66937c6242f90a637344d2ef275edd9c45e70c
|
|
| MD5 |
d055f1835b8aba3ff7b4bc8abab9f1d6
|
|
| BLAKE2b-256 |
ff5e86cb6da983aaf360d62c4cb18cf9dffbae1884916d3f32314d5c62e786ed
|
File details
Details for the file simple_injector-1.0.2-py3-none-any.whl.
File metadata
- Download URL: simple_injector-1.0.2-py3-none-any.whl
- Upload date:
- Size: 17.3 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
409ecbb20f4c73d462c5449bcf7a95b866e98a89a9f741148636f62d6b327f4d
|
|
| MD5 |
c386ad040966a1acec15b8d0a5b58bf1
|
|
| BLAKE2b-256 |
b7aa0326639af8926f6901002444e708fc2e6d3725dc922968660dd782cde080
|