Getting started¶
This page goes from installation to a working container. It assumes Python but
no prior depi knowledge.
Install¶
The core has no third-party runtime dependencies. Web adapters are covered in Integrations; the rest of this page is plain Python.
The smallest useful example¶
Three classes, where each depends on the one before it:
from depi import ServiceCollection
class Config:
def __init__(self):
self.greeting = "hello"
class Formatter:
def __init__(self, config: Config):
self.greeting = config.greeting
def format(self, name: str) -> str:
return f"{self.greeting}, {name}"
class Greeter:
def __init__(self, formatter: Formatter):
self.formatter = formatter
def greet(self, name: str) -> str:
return self.formatter.format(name)
depi reads the constructor annotations: Formatter needs Config, and
Greeter needs Formatter. No decorator or base class is involved.
Register the dependencies¶
services = ServiceCollection()
services.add_singleton(Config)
services.add_singleton(Formatter)
services.add_transient(Greeter)
ServiceCollection is a list of registrations. Each
add_* call says "when something needs this type, here is how to make it, and
here is how long the instance should live":
add_singleton— one instance for the life of the provider.add_transient— a fresh instance every time it is resolved.add_scoped— one instance per scope (see below).
Every constructor parameter must have a type annotation. An unannotated
parameter raises MissingAnnotationError at the
add_* call, not later:
class Broken:
def __init__(self, config): # no annotation
...
services.add_singleton(Broken)
# depi.MissingAnnotationError: Missing type annotation for parameter 'config' in Broken
Build the provider and resolve¶
provider = services.build_provider()
greeter = provider.resolve(Greeter)
print(greeter.greet("world")) # hello, world
build_provider() freezes the
registrations into a ServiceProvider. It also
validates the whole graph once, at this point:
- every constructor dependency is registered,
- there are no cycles,
- no singleton depends on a shorter-lived service.
A problem here raises a RegistrationError subclass,
so wiring mistakes fail at startup rather than on a request.
resolve(Greeter) constructs Greeter, which
needs a Formatter, which needs a Config. depi builds them in that order
and injects each one.
Lifetimes, concretely¶
provider = services.build_provider()
a = provider.resolve(Greeter)
b = provider.resolve(Greeter)
assert a is not b # transient: different objects
assert a.formatter is b.formatter # singleton: shared Formatter
Greeter is transient, so each resolve call returns a new one. Formatter
and Config are singletons, so the two greeters share the same instances.
Scoped lifetime¶
A scope is a bounded region — typically one HTTP request — within which scoped services behave like singletons, and after which they are disposed.
from depi import ServiceCollection
class RequestId:
_next = 0
def __init__(self):
RequestId._next += 1
self.value = RequestId._next
services = ServiceCollection()
services.add_scoped(RequestId)
provider = services.build_provider()
with provider.create_scope() as scope:
first = scope.resolve(RequestId)
second = scope.resolve(RequestId)
assert first is second # same instance within the scope
with provider.create_scope() as scope:
assert scope.resolve(RequestId) is not first # a different scope, a new instance
Resolving a scoped service straight from the provider — with no scope — raises
ScopeRequiredError. Scopes are context managers;
when the with block exits, any scoped instance that defines a dispose()
method has it called. async with is also supported and
awaits async cleanup.
Next, build a small app in the Tutorial, read the full behavioral model under Concepts, or see where the container belongs in Architecture.