Lagom, a type based dependency injection container
Project description
Lagom - Dependency injection container
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]
Defining a singleton
container[SomeExpensiveToCreateClass] = SomeExpensiveToCreateClass("up", "left")
alternatively if you want to defer construction until it's needed:
container[SomeExpensiveToCreateClass] = Singleton(SomeExpensiveToCreateClass)
Defining a type that gets recreated every time
container[SomeClass] = lambda: SomeClass("down", "spiral")
Alias a concrete instance to an ABC
container[SomeAbc] = ConcreteClass
Example
from abc import ABC
from dataclasses import dataclass
from lagom import Container
#--------------------------------------------------------------
# Here is an example of some classes your application may be built from
@dataclass
class DiceApiUrl:
url: str
class RateLimitingConfig:
pass
class DiceClient(ABC):
pass
class HttpDiceClient(DiceClient):
def __init__(self, url: DiceApiUrl, limiting: RateLimitingConfig):
pass
class Game:
def __init__(self, dice_roller: DiceClient):
pass
#--------------------------------------------------------------
# Next we setup some definitions
container = Container()
# We need a specific url
container[DiceApiUrl] = DiceApiUrl("https://roll.diceapi.com")
# Wherever our code wants a DiceClient we get the http one
container[DiceClient] = HttpDiceClient
#--------------------------------------------------------------
# Now the container can build the game object
game = container[Game]
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
lagom-0.1.1.tar.gz
(10.8 kB
view hashes)
Built Distribution
Close
Hashes for lagom-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4800cc9016cb0fbe37c9642f423b049b31bdeee8be0ad6f0f9e3c95263e99e5b |
|
MD5 | abf5e753ecc98ad88000b71fc41f6f7b |
|
BLAKE2b-256 | 23804d0ad9a911be1b8b3b33fbad3b4b3c22ce21c81329ee70c69834ec88c053 |