03 · Get Started
Five-Minute Quickstart
You don't need to sketch out a folder hierarchy or register classes in a central file just to get moving. Let's build a working graph in three quick steps: wrap plain functions with provide(), boot them with bootstrapApp(), and swap an async dependency for a fake in a test.
1. Define Providers with provide()
Pass any factory function to provide() and you get back a typed Ref<T> handle. In step1-provide.ts, configRef returns a plain object, and greeterRef pulls it in via { inject } to build a greet() helper.
Notice two things right away:
- Zero manual type annotations: TypeScript infers
configinsidegreeterRefstraight fromconfigRef's return type. - Nothing has executed yet: Calling
provide()only records how to construct the value when an app eventually asks for it.
1import { provide } from 'dn-ioc'23export const configRef = provide(() => ({ greeting: 'hello' }))45export const greeterRef = provide(({ inject }) => {6 const config = inject(configRef)7 return {8 greet: (name: string) => `${config.greeting}, ${name}`,9 }10})
2. Boot the Graph with bootstrapApp()
Next, hand a root function to bootstrapApp() and call await app.start(). bootstrapApp() creates an isolated app scope and returns the app handle immediately—still without running any factory.
When await app.start() runs:
- Your root function asks for
inject(greeterRef). greeterRefself-installs in the app scope and asks forinject(configRef).configRefruns once, caches{ greeting: 'hello' }, andgreeterRefcaches its{ greet }object and logshello, world.
Because this little graph holds no open sockets, timers, or database pools, you don't need app.stop()—once start() finishes, normal garbage collection cleans everything up.
1import { bootstrapApp } from 'dn-ioc'2import { greeterRef } from './step1-provide'34// bootstrapApp() builds the graph handle without running any factory yet5export const app = bootstrapApp(({ inject }) => {6 const greeter = inject(greeterRef)7 console.log(greeter.greet('world')) // hello, world8})910await app.start()
3. Go Async and Swap a Test Fake
Real services usually load settings over the network or talk to a database, and tests need to swap those out cleanly. Both take a single line of wiring:
- Async providers use regular
asyncfunctions: MakesettingsRefasyncandinject(settingsRef)returns a sharedPromisethatgreeterRefawaits. MarkingbootstrapApp's root functionasyncand awaitinginject(greeterRef)ensuresawait testApp.start()waits until startup settles. provideFor()swaps any dependency in place: PassingprovideFor(settingsRef, async () => ({ greeting: 'test-hello' }))in{ providers }replacessettingsReffortestAppwithout touchinggreeterRef's code.- Clean test teardown: Calling
await testApp.stop()closes injection and runs any registered cleanup hooks so resources don't bleed into the next test.
1import { bootstrapApp, provide, provideFor } from 'dn-ioc'23export const settingsRef = provide(async () => ({ greeting: 'hello' }))45export const greeterRef = provide(async ({ inject }) => {6 const settings = await inject(settingsRef)7 return {8 greet: (name: string) => `${settings.greeting}, ${name}`,9 }10})1112export const testApp = bootstrapApp(13 async ({ inject }) => {14 const greeter = await inject(greeterRef)15 console.log(greeter.greet('Ada')) // test-hello, Ada16 },17 {18 providers: [provideFor(settingsRef, async () => ({ greeting: 'test-hello' }))],19 },20)2122await testApp.start()23await testApp.stop()
Habits Worth Remembering
Next up, we'll look under the hood at Providers & Refs and Tokens & Binding—or if you're eager to structure a full codebase, jump straight to the MVC or DDD & Clean Architecture guides.