Register it#
A provider is available to its own module as soon as it is listed:
@Module(controllers=[TaskController], providers=[TaskService])
class TaskModule:
pass
Share it with another module#
Providers are private to their module by default. To let another module use one, export it:
@Module(
controllers=[TaskController],
providers=[TaskService],
exports=[TaskService],
)
class TaskModule:
pass
Then import the module that owns it:
@Module(imports=[TaskModule], controllers=[ReportController])
class ReportModule:
pass
ReportController can now ask for TaskService. Importing a module gives you
its exports, not its internals.
Provide a value or a factory#
For configuration, or anything not constructed by calling a class:
from bustan import Module, Provider
def make_client(settings: Settings) -> ApiClient:
return ApiClient(base_url=settings.api_url, timeout=5.0)
@Module(
providers=[
Provider(token="SETTINGS", value=Settings()),
Provider(provide=ApiClient, factory=make_client, inject=[Settings]),
],
exports=[ApiClient],
)
class ClientModule:
pass
Inject by token#
When the dependency is not a class:
from bustan import Inject
@Injectable()
class ReportService:
def __init__(self, settings = Inject("SETTINGS")) -> None:
self.settings = settings
Common error#
BustanResolutionError: TaskService is not available to ReportController.
ReportModule imports [TaskModule]
TaskModule exports []
hint: add TaskService to TaskModule's exports
This almost always means the provider was registered but not exported.