A simple library for typing-based dependency injection
Project description
Simple Injection
Introduction
Simple Injection is a simple, objected-oriented approach to dependency injection in python. The goal of Simple Injection is to allow simple and effective dependency injection in python applications without the use of anything other than what is natively in your application. This means no decorators or anything else anywhere in your code is required for Simple Injection. All that is required is python typings.
Features
- Only dependent on native python code and typings. Services are injected through typing annotations, rather than variable names or decorators in your code.
- Easily define service lifetimes.
- Enforces typings in your application.
- Bind abstract services to their implementations.
- Add services in any order, resolution occurs when a service is request.
Installation
Simply install Simple Injection through pip.
pip install simple-injection
Usage
Due to Simple Injection relying only on typings, it is easy to add to your application.
from simple_injection import ServiceCollection
class Dependency:
def hello(self):
print("Hello from Dependency!")
class Service:
def __init__(self, dependency: Dependency):
self._dependency = dependency
def hello(self):
self._dependency.hello()
collection = ServiceCollection()
collection.add_transient(Dependency)
collection.add_transient(Service)
collection.resolve(Service).hello()
# Outputs: Hello from Dependency!
This approach to dependency injection makes it easy to use mocks when developing and unit testing.
class MockDependency:
def hello(self):
print("Hello from MockDependency!")
collection = ServiceCollection()
collection.add_transient(Dependency, MockDependency)
collection.add_transient(Service)
collection.resolve(Service).hello()
# Outputs: Hello from MockDependency!
This can also be achieved through the use of an interface (or base class) that both the dependency and the mock inherit from, but as the above example shows, it is not required.
Simple Injection will also allow you to simply inject strings and other constants to your dependencies, easily injecting the needed dependency to your class with the constant.
from simple_injection import ServiceCollection, ServiceResolverFlags
class Dependency:
def __init__(self, my_str: str):
self.my_str = my_str
class Service:
def __init__(self, dependency: Dependency, my_int: int):
self.my_int
self._dependency = dependency
def get_str(self):
return self._dependency.my_str
collection = ServiceCollection()
collection.add_transient(Dependency, args=["Example string!"])
collection.add_transient(Service, args=[ServiceResolverFlags.REQUIRED_SERVICE ,23])
service = collection.resolve(Service)
service.my_int # 23
service.get_str() # Example string!
See examples for more examples.
Documentation
Documentation for Simple Injection can be found on readthedocs.
Contributing
Contributions are more than welcome. Feel welcome to add issues or make pull requests!
License
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
File details
Details for the file simple-injection-0.2.5.tar.gz
.
File metadata
- Download URL: simple-injection-0.2.5.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c2e7943cb99cefafe451517098afeccb1de635bf81e686414dc3710f50929f4 |
|
MD5 | 01862a88e1c49987de565f1af5bd4e3a |
|
BLAKE2b-256 | 956e906c5af858f6741bc7046c6348104f3a261915fd46a2a186171a3e3e301e |
File details
Details for the file simple_injection-0.2.5-py3-none-any.whl
.
File metadata
- Download URL: simple_injection-0.2.5-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7facbe055910e2535db0959c0dd218b5c82f33d235f92311d172cfebc9a12921 |
|
MD5 | 1ad506db43a0bfaddfb980cf6ae17541 |
|
BLAKE2b-256 | b0153359c8226411cf9bca3fca817aedbb5d66331675dd5605d35e2b126a34cb |