cobalt 0.1.2 copy "cobalt: ^0.1.2" to clipboard
cobalt: ^0.1.2 copied to clipboard

Runtime core of Cobalt, a dependency injection framework with hierarchical scopes, deterministic disposal and async init graphs.

example/example.md

cobalt example #

The runtime, with no code generation — Manual Mode. The generator writes exactly this, using only what is exported here.

import 'package:cobalt/cobalt.dart';

class Database {
  var isOpen = false;
}

final class DatabaseFactory implements CobaltFactory<Database> {
  const DatabaseFactory();

  @override
  Database create(CobaltResolver resolver) => Database();
}

class AppScope implements CobaltScopeBuilder {
  const AppScope();

  @override
  void build(CobaltScope scope) =>
      scope.registerLazySingleton<Database>(const DatabaseFactory());
}

Future<void> main() async {
  final app = await CobaltApplication.start(
    root: const AppScope(),
    rootName: 'app',
  );

  print(app.get<Database>());

  // A child scope: everything built inside dies with it, in reverse order of
  // creation. This is what makes sign-out a single call.
  final session = app.push('session:42');
  await session.init();
  await session.dispose();

  await app.dispose();
}

More: examples/manual_mode.

1
likes
160
points
230
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Runtime core of Cobalt, a dependency injection framework with hierarchical scopes, deterministic disposal and async init graphs.

Repository (GitHub)
View/report issues

Topics

#dependency-injection #di #service-locator #scopes

License

MIT (license)

Dependencies

cobalt_annotations, collection, meta

More

Packages that depend on cobalt