Dependency Injection

AIOBoot uses injector - Dependency injection framework for managing application dependencies using type annotations.

First component

from aioboot.applications import Application

app = Application()


class Component:
    def __init__(self, name: str):
        self.name = name


# Bind component

app.bind(Component, to=Component(name="db"))

# Lookup component

component = app.lookup(Component)

print(f"Component name: {component.name}")

Prints out:

>>> Component name: db

Scopes

Factory scope (noscope)

from aioboot.applications import Application

app = Application()


class Factory:
    pass


app.bind(Factory)

print(app.lookup(Factory) is app.lookup(Factory))

Prints out:

>>> False

Singleton scope

from aioboot.applications import Application
from aioboot.dependency import singleton

app = Application()


class Factory:
    pass


app.bind(Factory, scope=singleton)

print(app.lookup(Factory) is app.lookup(Factory))

Prints out:

>>> True