Lifecycles
Compdi models dependency lifetime explicitly in the macro that defines it.
| Need | Use | Behavior |
|---|---|---|
| One value immediately | createSingleton | Constructs and exports one shared value. |
| One value behind an accessor | defineSingleton | Returns one shared value and supports lazy initialization. |
| A fresh value per operation | createTransient or defineTransient | Returns a factory that constructs on every call. |
| A value selected from ambient context | createScoped | Returns a stable proxy and lifecycle controller. |
| A value selected by an explicit key | defineScoped | Returns a per-context accessor with lifecycle methods. |
| Full-application cleanup | defineAppTeardown | Experimental reverse-order disposal; may be removed. |
Use the narrowest lifecycle that represents the ownership of a value. Shared infrastructure such as configuration and database pools is commonly singleton-scoped. Stateful operation objects are usually transient. Request- or tenant-specific state belongs in a scoped dependency.
A practical default
Start with createSingleton for shared infrastructure and createTransient for stateful work. Introduce a scoped dependency only when the same context must reuse a value and different contexts must remain isolated.
Continue with targets, factories, and inference, or go directly to scoped dependencies.