Learn

Learn Ark DI

Understand immutable registration, contextual resolution, scopes, ownership, async initialization, and teardown.

v1.1.0Beginner

Learn Ark DI

Ark DI is a pure-Dart object-graph runtime. It owns composition mechanics, not business behavior and not a hidden global access point.

Start with the problem

An application must create objects in dependency order: configuration before an API client, the client before a Repository, and the Repository before a service or UseCase. Some objects are shared, some are transient, some initialize asynchronously, and some must be disposed in the reverse order. Tests and feature previews may also need bounded replacements without mutating the production graph.

Ark DI records those construction and lifecycle rules in one immutable container:

text
Composition Root registers construction rules
        ↓
Resolver creates or returns a dependency according to its strategy
        ↓
child Scope may provide explicit local overrides
        ↓
close waits for active resolution and disposes owned objects in dependency order

Application classes should still use constructor injection. The container belongs at composition boundaries; ordinary domain and application code does not retrieve it.

Choose a registration by lifetime

NeedRegistration
Existing object with explicit ownerInstance
New short-lived object for every requestFactory
One object created on first useLazy Singleton
New asynchronous object for every requestAsync Factory
One shared asynchronously initialized objectAsync Lazy Singleton
  1. Getting started builds and closes a small graph.
  2. Composition Root explains where concrete types are connected.
  3. Registrations and ownership separates acquisition from lifecycle responsibility.
  4. Scopes and overrides defines hierarchy and local-first resolution.
  5. Async resolution covers deterministic access, retries, and shared in-flight initialization.
  6. Lifecycle and teardown explains active leases and reverse dependency order.

Flutter integration is a separate branch backed by ark_di_flutter.

Ark DI does not require a single application-wide Singleton container. A root Scope is available for applications that need it, while feature, session, preview, and test Scopes keep shorter lifetimes explicit.