darto_inject library

Typed dependency injection for Darto.

import 'package:darto/darto.dart';
import 'package:darto_inject/darto_inject.dart';

final envProvider = Provider<Env>((di) => Env.fromFile('.env'));
final dbProvider  = Provider<Db>(
  (di) => Db.connect(di.read(envProvider).dbUrl),
  onDispose: (db) => db.close(),
);

final di = Di(providers: [envProvider, dbProvider]);
await di.warmup();

final app = Darto()..use(di.middleware());
app.get('/health', [], (c) => c.ok({'db': c.read(dbProvider).pingedAt}));

Classes

AsyncProvider<T>
A typed factory for an asynchronous value of type T.
Di
Holds factories, per-scope caches and disposers.
DiRef
Reference passed to a provider factory — gives the factory typed access to other providers in the same container.
Feature
A self-contained slice of an app: the providers it owns and the routes that use them. Install one (or many) into a Darto with app.install(feature).
Provider<T>
A typed factory for a value of type T.

Enums

Scope
Lifetime of a provider's cached instance.

Extensions

ContextDi on Context
Reads providers off the current request — the main user-facing API.
DartoFeature on Darto
Wires Features into a Darto app.

Properties

contextProvider Provider<Context>
Built-in Provider that yields the current request's Context. Only readable from a request-scoped factory — otherwise there is no Context to bind to.
final

Functions

collectProviders(Iterable<Feature> features) → (List<Provider>, List<AsyncProvider>)
Flattens the providers of every feature in features into the two lists expected by Di. Convenient at app boot:

Typedefs

FeatureRoutes = void Function(Router r)
Builder callback used by Feature.routes — receives a Router already prefixed by whatever path was passed to DartoFeature.install (or the root router when no prefix is given). Mirrors the callback shape of app.route(prefix, builder).