On this page
Flat overrides and live creation probes๏
Prerequisite: addressing and
lifetimes. A flat override supplies constructor inputs
for the object being resolved. A factory can instead capture configuration at its
registration site. The saved mailer lesson demonstrates both with concrete values.
Choose where configuration belongs๏
The factory returns a mailer configured for its fixed environment. The separately
registered many class accepts host and port through meld(override={...}).
Its assertions compare both resulting configurations.
An override is not a new binding or a global configuration edit. Pair the choice with the intended instance lifetime; repeated reuse and fresh construction are different operations. Follow the complete script's lifetime choices first.
Inspect before constructing๏
has_live_creation(...) asks whether an addressed object already exists.
The live-probe lesson checks False before its first meld and True afterward,
then uses describe_live_creation_status(...) for detail. A diagnostic path can
use these reads without using a meld merely to discover whether something is alive.
Continue to deep overrides when the target is inside a dependency graph rather than a parameter of the object you are resolving.
The workflow in code๏
These are the core steps from the saved example. Use its complete linked script for all class definitions and setup.
1def main() -> None:
2 book = md.Spellbook()
3 book.bind(spell=production_mailer, existence="unique",
4 spellframe="mail", binding_name="mailer")
5 book.bind(spell=SmtpMailer, existence="many",
6 spellframe="mail", binding_name="raw-mailer")
7 conduit = book.conjure()
8
9 mailer = conduit.meld(spellframe="mail", binding_name="mailer")
10 assert (mailer.host, mailer.port) == ("smtp.example.com", 2525)
11 print("factory-configured:", mailer.host, mailer.port)
12
13 overridden = conduit.meld(
14 spellframe="mail", binding_name="raw-mailer",
15 override={"host": "smtp.test.local", "port": 1025},
16 )
17 assert (overridden.host, overridden.port) == ("smtp.test.local", 1025)
18 print("meld-site override:", overridden.host, overridden.port)
Runnable examples๏
Spell override construction โ Intermediate 08
Live creation probe โ Intermediate 18
All intermediate examples ยท Level contents ยท Full contents