Lagom, a type based dependency injection container
Project description
What
Lagom is a dependency injection container designed to give you "just enough" help with building your dependencies. The intention is that almost all of your code doesn't know about or rely on lagom. Lagom will only be involved at the top level to pull everything together.
Features
- Typed based auto wiring with zero configuration.
- Fully based on types. Strong integration with mypy.
- Minimal changes to existing code.
- Integration with a few common web frameworks.
- Support for async python.
- Thread-safe at runtime
You can see a comparison to other frameworks here
Installation
pip install lagom
# or:
# pipenv install lagom
# poetry add lagom
Note: if you decide to clone from source then make sure you use the latest version tag. The master
branch may contain features that will be removed.
For the versioning policy read here: SemVer in Lagom
Usage
Everything in Lagom is based on types. To create an object you pass the type to the container:
container = Container()
some_thing = container[SomeClass]
Auto-wiring (with zero configuration)
Most of the time Lagom doesn't need to be told how to build your classes. If
the __init__
method has type hints then lagom will use these to inject
the correct dependencies. The following will work without any special configuration:
class MyDataSource:
pass
class SomeClass:
def __init__(datasource: MyDataSource)
pass
container = Container()
some_thing = container[SomeClass] # An instance of SomeClass will be built with an instance of MyDataSource provided
and later if you extend your class no changes are needed to lagom:
class SomeClass:
def __init__(datasource: MyDataSource, service: SomeFeatureProvider)
pass
# Note the following code is unchaged
container = Container()
some_thing = container[SomeClass] # An instance of SomeClass will be built with an instance of MyDataSource provided
Singletons
You can tell the container that something should be a singleton:
container[SomeExpensiveToCreateClass] = SomeExpensiveToCreateClass("up", "left")
Explicit build instructions when required
You can explicitly tell the container how to construct something by giving it a function:
container[SomeClass] = lambda: SomeClass("down", "spiral")
All of this is done without modifying any of your classes. This is one of the design goals of lagom.
Hooks in to existing systems
A decorator is provided to hook top level functions into the container.
@bind_to_container(container)
def handle_move_post_request(request: typing.Dict, game: Game = lagom.injectable):
# do something to the game
return Response()
(There's also a few common framework integrations provided here)
Contributing
Contributions are very welcome. Please see instructions here
Project details
Release history Release notifications | RSS feed
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
Hashes for lagom-1.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1369f1ced05528b12460f7e9e4162c4c1edb68bda76ae454c6c2066bc093b023 |
|
MD5 | 1d69711ad69698a194c32a81c93b492d |
|
BLAKE2b-256 | 9917ed51f60bba27721a6470c98991595648e2c21de1a79c4049f2e9a681c8f1 |