---
myst:
html_meta:
description: "Learn constructor dependency injection in Melder and follow a complete runnable Python example with typed dependencies."
---
# Constructor dependency injection
**Beginner → Intermediate.** Annotations, explicit targets, collections.
Annotate a constructor parameter. Melder builds the graph.
What an annotation selects depends on what it is. A **class** selects only the spell of that class bound
**bare** — no `spellframe` and no `binding_name`, the class itself or an existing object of it — which is
exactly what `meld(TheClass)` returns. A spell bound under a spellframe is reached through its frame, and a
spell bound under a binding name through that name (`SpellMap(TheClass, binding_name=...)`); a category that
happens to share the class's name does not turn its members into providers either. When the class is bound
only under a spellframe or a binding name, `conjure()` refuses the consumer with `HIDDEN_PROVIDER`, naming
each place it is bound and the `SpellMap` that reaches it. A **Protocol** selects
the spells bound under it as their contract. A **`TYPE_CHECKING`-only import**, which leaves a string at
runtime, resolves by name to a bare type or a contract, never to a string category. `list[...]` gathers the
group the annotation's kind names: the bare spell of the class, every implementer of the Protocol, or — for
a string — every member of the category or type of that name.
```python
import melder as md
class Database:
"""A small stand-in for a database; this example performs no I/O."""
def query(self) -> str:
return "42 rows"
class ReportService:
def __init__(self, database: Database) -> None: # ← injected
self.database = database
def run(self) -> str:
return "report over " + self.database.query()
book = md.Spellbook()
book.bind(spell=Database, existence="unique")
book.bind(spell=ReportService, existence="unique")
with book.conjure() as conduit:
report: ReportService = conduit.meld("ReportService")
assert isinstance(report.database, Database)
print(report.run())
```
This complete example prints `report over 42 rows`: melding the consumer first
constructs its registered dependency and injects it into the constructor.
## Runnable examples
- [Constructor di by annotation](../examples/intermediate/15-constructor-di-by-annotation.md) — Intermediate 15
- [Typed melding](../examples/beginner/16-typed-melding.md) — Beginner 16
[All beginner examples](../examples/beginner/index.md) · [Level contents](index.md) · [Full contents](../contents.md)
[Canonical page source](https://github.com/Synaptic724/melder/blob/0242cd0083dad7c225d112258e328a3f5b39461a/README.md)