On this page

Bootstrap pattern๏ƒ

๐ŸŸข Beginner ยท Lesson 18

The composition root - ONE function that binds the whole world and hands back the conduit. Configured things are BUILT there and bound as instances; everything else in the app just melds. Note: melder worlds do not "switch environments" - the same objects cannot register twice (a spell_id is the SHA256 of an object's code and data), so your MODULE decides what to build, once, at the root.

Before you run๏ƒ

Use the Beginner guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/01_beginner/18_bootstrap_pattern.py
py -3.14t UX_and_AIX_experiences/01_beginner/18_bootstrap_pattern.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

the build_world() pattern, instance spells

Code๏ƒ

 1"""
 2TIER: beginner (18)
 3GOAL: The composition root - ONE function that binds the whole world
 4      and hands back the conduit. Configured things are BUILT there and
 5      bound as instances; everything else in the app just melds. Note:
 6      melder worlds do not "switch environments" - the same objects
 7      cannot register twice (a spell_id is the SHA256 of an object's code
 8      and data), so your MODULE decides what to build, once, at the root.
 9SURFACE EXERCISED: the build_world() pattern, instance spells
10"""
11import melder as md
12
13
14class Settings:
15    def __init__(self, database_url: str, retries: int) -> None:
16        self.database_url = database_url
17        self.retries = retries
18
19
20class Api:
21    pass
22
23
24def build_world() -> tuple[md.Spellbook, md.Conduit]:
25    """The one place construction knowledge lives."""
26    book = md.Spellbook()
27    book.bind(spell=Settings("postgres://db.example:5432/app", 3),
28              existence="unique",
29              spellframe="app", binding_name="settings")
30    book.bind(spell=Api, existence="many")
31    return book, book.conjure()
32
33
34def main() -> None:
35    book, conduit = build_world()
36    settings = conduit.meld(spellframe="app", binding_name="settings")
37    assert settings.retries == 3
38    print("world bootstrapped:", settings.database_url)
39    print("app code melds; only build_world() knows how things are made")
40    assert len(book.spells) == 2
41
42
43if __name__ == "__main__":
44    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More beginner examples ยท Level guide