# Constructor, explicit, and collection DI **Beginner → Intermediate.** Annotations, explicit targets, collections. Annotate a constructor parameter. Melder builds the graph. ```python class ReportService: def __init__(self, database: Database) -> None: # ← injected self.database = database book.bind(spell=Database, existence="unique") book.bind(spell=ReportService, existence="unique") conduit = book.conjure() report = conduit.meld("ReportService") ``` When an annotation isn't specific enough — two implementations of one shape, or a named binding — declare the target with `SpellMap` as the parameter default: ```python class Consumer: def __init__(self, store=md.SpellMap(PrimaryStore)) -> None: self.store = store ``` `SpellMap` takes the full address: a concrete spell, a `spellframe`, a `binding_name`, or a combination. It obeys an **exactly-one law** — a target that matches zero spells or several fails at `conjure()`, not at runtime. Ask for a list and get every implementation — the plugin pattern in one annotation: ```python class Dispatcher: def __init__(self, handlers: list[Handler]) -> None: # ← all of them self.handlers = handlers book.bind(spell=EmailHandler, existence="unique", spellframe=Handler, binding_name="email") book.bind(spell=SmsHandler, existence="unique", spellframe=Handler, binding_name="sms") ``` ## Runnable examples - [Constructor di by annotation](../examples/intermediate/15-constructor-di-by-annotation.md) — Intermediate 15 - [Spellmap defaults](../examples/intermediate/16-spellmap-defaults.md) — Intermediate 16 - [Collection di](../examples/intermediate/17-collection-di.md) — Intermediate 17 [All intermediate examples](../examples/intermediate/index.md) · [Level contents](index.md) · [Full contents](../contents.md) [Canonical page source](https://github.com/Synaptic724/melder/blob/a30a754d0bb61db0b142936010cdb82cffecec6f/README.md)