dino 1.0.0-pre.1 copy "dino: ^1.0.0-pre.1" to clipboard
dino: ^1.0.0-pre.1 copied to clipboard

A Dart dependency injection library aimed to be flexible, predictable and easy to use.

example/lib/main.dart

import 'package:dino/dino.dart';

class DependencyA {}

class DependencyB {}

abstract class TestService {
  void doSomething();
}

class TestServiceImpl implements TestService {
  final DependencyA dependencyA;
  final DependencyB dependencyB;

  TestServiceImpl(this.dependencyA, this.dependencyB);

  @override
  void doSomething() {
    print('doSomething');
  }
}

void main() {
  final services = ServiceCollection();

  // Create dependency A only once
  services.addInstance(DependencyA());

  // Create dependency B every time it is requested
  services.addTransientFactory((sp) => DependencyB());

  // Create TestService per a scope
  services.addScopedFactory(
    (sp) => TestServiceImpl(
      sp.getRequired<DependencyA>(),
      sp.getRequired<DependencyB>(),
    ),
  );

  // Add an alias for TestService
  services.addAlias<TestService, TestServiceImpl>();

  // Create a root scope
  final rootScope = services.buildRootScope();

  // Create a nested scope
  final scope = rootScope.serviceProvider.createScope();

  // Resolve TestService from the nested scope
  final testService = scope.serviceProvider.getRequired<TestService>();

  testService.doSomething();
}
2
likes
150
pub points
43%
popularity

Publisher

verified publisherexeteres.dev

A Dart dependency injection library aimed to be flexible, predictable and easy to use.

Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (license)

More

Packages that depend on dino